Talking about the use of Foreach/in_array in PHP

Source: Internet
Author: User
Tags first string
PHP is very efficient in development, which is understandable, but at the expense of execution efficiency. PHP Array function is very powerful, but also to consider more, try a few situations, just in case, here, I simply say two encounters of the pit, later if there are found more, then fill it!

foreach provides a simple way to iterate through an array, and it is easy to read the contents of the data or object, but the official document says that because foreach relies on an internal array pointer, modifying its value in a loop can lead to unexpected behavior. So, basically,
1, do not want to change inside the loop within the value, otherwise the result will be more than you want;
2, the use of ' & ' is a safe way, although rarely used, but when used, at the end of the reference should be called immediately after the unset function to destroy the variable, otherwise, in the next code, if you have to use this variable again, then the last value of the loop will be modified, resulting in unexpected values, For example: In the list loop output, the last line of output will appear a lot of messy values or null values. Use unset to resolve this issue.

Let's look at an example.

<?php/*-------------------------------------------------------------------------*//* foreach Example 1:value Only */echo "foreach example 1:value only". ' <br/> '; $a = Array (1, 2, 3, 17); foreach ($a as $v) {echo "Current value of". $a. ":". $v. " <br/> ";  }?>//Run result foreach example 1:value only current value of $a: 1 Current value of $a: 2 current value of $a: 3 current Value of $a: 17

In_array, the meaning is to check whether the previous string exists in the latter array, and in most cases, it works, but when the following array is an integer, such as Array (0,1,2,3), there is a problem, PHP will be the former string intval, So you get a value of 0, so if you happen to have this value in your array, then the equation is set up, is it more than expected?
So, when you're sure that the data that follows is an integer, especially if it's probably 0 (which might replace all of the strings), you can't use this function anymore, but you can use key_exists instead, but the data behind it needs to be reversed using Array_flip.

Let's look at another example.

function Search ($keyWord, $stack) {///here to determine whether you should update or insert a       foreach ($stack as $key + $val) {         if (In_array ($keyWord, $ Val) {           return TRUE;         }       }       return FALSE;     }

What happens when a string is used as an array to fetch values? PHP is a very fault-tolerant language, it will try to help you correct the error, so it is very clever to convert your reference subscript into an integer, of course, you get 0, then the string subscript 0 to get the value of the first string, is it more than you expected? The workaround is to confirm that the variable is not an array before referencing the subscript, Is_array.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.