Talking about the use of Foreach/in_array in PHP, foreachin_array_php tutorial

Source: Internet
Author: User
Tags first string

Talking about the use of Foreach/in_array in PHP, Foreachin_array


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".
'; $a = Array (1, 2, 3, 17); foreach ($a as $v) {echo "Current value of". $a. ":". $v. "

  In_array, the meaning is to check whether the previous string exists in the latter array, and most of the time it works, but when the subsequent array is an integer, such as Array (0,1,2,3), there is a problem. PHP will intval the previous string so that it will get a value of 0, so if it happens that you have this value in the 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.

Although there are many of the predecessors encountered such or such problems, but also kept saying, but we are inevitably unavoidable, and continue to make mistakes. This may be the society! Everyone is so busy, there is no time to crack your bad code! Ha ha

http://www.bkjia.com/PHPjc/1067302.html www.bkjia.com true http://www.bkjia.com/PHPjc/1067302.html techarticle talking about the use of Foreach/in_array in PHP, Foreachin_array PHP in the development of high efficiency, this is understandable, but at the expense of execution efficiency. PHP arrays are very powerful, but ...

  • 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.