Talking about the use _php skill of foreach/in_array in PHP

Source: Internet
Author: User
Tags first string

PHP is very efficient in development, this is understandable, but it is at the expense of implementation efficiency. PHP Array function is very powerful, but also need to consider more, try several situations, just in case, here, I simply said that two encountered the pit, later if there are more to find, and then fill it!

  foreach provides a simple way to traverse an array to easily read the contents of data or objects, but the official document says that because foreach relies on an internal array pointer, modifying its value in a loop can result in 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, however, when used, the unset function should be called immediately after the reference is destroyed, otherwise the last value of the loop will be modified to get an unexpected value if it is used again in the next code. For example, when the list loops out, the last line of output will appear with a lot of messy values or null values. Use unset to resolve this issue.

Let's take a look at an example

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

  In_array, which means checking whether the previous string exists in the next array, and in most cases it works, but when the following array is an integer, such as Array (0,1,2,3), the problem occurs. PHP intval The previous string so that it gets a value of 0, so if you happen to have this value in your array, then the equation is set up, is it going beyond expectations?
So, when you're sure that the next data is an integer, especially if it's probably 0 (this might replace all the strings), you can never use the function again, but use key_exists instead, but the following data will need to be inverted using array_flip.

Let's take a look at another example.

function Search ($keyWord, $stack) {//This determines whether to 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 highly fault tolerant language, it will try to help you correct the error, so it is smart to convert your reference to an integer, of course, you get 0, then the string subscript 0 values will get the value of the first string, is not beyond your expectations? The solution is to confirm that the variable is not an array before referencing the subscript, Is_array.

Although many of the predecessors encountered such or such a problem, but also kept saying, but we are unavoidable, and constantly to make mistakes. This may be the Society bar! Everyone is so busy, there is no time to crack your rotten code! Ha ha

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.