Home > Backend Development > PHP Tutorial > A brief discussion on the use of foreach/in_array in PHP, foreachin_array_PHP tutorial

A brief discussion on the use of foreach/in_array in PHP, foreachin_array_PHP tutorial

WBOY
Release: 2016-07-12 09:06:00
Original
906 people have browsed it

A brief discussion on the use of foreach/in_array in PHP, foreachin_array

PHP is very efficient in development, which is understandable, but it sacrifices execution efficiency. The php array function is very powerful, but you should also consider it more and try several situations just in case. Here, I will briefly mention the two pitfalls encountered. If more are found in the future, I will add them. !

 foreach provides a simple way to traverse the array and can easily read the contents of the data or object. However, the official documentation says that because foreach relies on the internal array pointer, its value is modified in the loop. may cause unexpected behavior. So, basically,
1. Don’t try to modify the value inside the loop, otherwise the result will exceed what you want;
2. Using '&' is a safe way. Although it is rarely used, when it is used, the unset function should be called immediately after the reference ends to destroy the variable. Otherwise, in the following code, if there is another If this variable is used, the last value of the loop will be modified, resulting in unexpected values. For example, when the list loop is output, many messy values ​​or null values ​​will appear in the last line of output. Using unset solves this problem.

Let’s take a look at an example

<&#63;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 />"; 
} 
&#63;> 
// 运行结果 
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 
Copy after login

 in_array,The meaning is to check whether the previous string exists in the following array, and in most cases, it also works like this, but when the following array is an integer, such as array (0,1,2,3), there is a problem. PHP will intval the previous string, so you will get the value 0. Then if by chance, your array has this value, then the equation will be established. , did it exceed expectations again?
Therefore, when it is determined that the following data is an integer, especially if it may be 0 (this may replace all strings), you must not use this function anymore. You can use key_exists instead, but the following data You need to use array_flip to perform the inversion operation.

Let’s look at another example

function search($keyWord, $stack) {//此处判断是应该更新还是插入

      foreach ($stack as $key => $val) {

        if (in_array($keyWord, $val)) {

          return TRUE;

        }

      }

      return FALSE;

    }
Copy after login

What happens when a string is used as an array to get values? PHP is a very fault-tolerant language. It will try its best to help you correct your mistakes, so it smartly converts your reference subscript into an integer. Of course, you get 0. Then the string subscript is 0 and the value is You will get the value of the first string. Is it beyond your expectation? The solution is to confirm whether the variable is an array before referencing the subscript, is_array.

Although there are many seniors who have encountered this or that problem and kept talking about it, we are still inevitable and keep making mistakes after all. Maybe this is society! Everyone is so busy, how can we have time to crack your crappy code! Haha

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1067302.htmlTechArticleA brief discussion on the use of foreach/in_array in PHP, foreachin_array PHP is very efficient in development, which is understandable, but But it is sacrificing execution efficiency. PHP arrays are very powerful, but...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template