Introduction to the use of foreach/in_array in PHP

Source: Internet
Author: User
This article briefly summarizes the use of foreach and in_array in PHP, including frequently encountered problems and other information. it is very practical. if you need it, you can refer to it. Php is very efficient in development, which is understandable, but at the cost of execution efficiency. The php array function is very powerful, but you need to consider it more. try several cases more, just in case, here, I will simply talk about two pitfalls. if you find more problems in the future, try again!

  ForeachProvides a simple way to traverse arrays and easily read data or object content, but the official documentation says that because foreach depends on internal array pointers, modifying its value in a loop may lead to unexpected behavior. Therefore, basically,
1. do not modify the value inside the loop; otherwise, the result will exceed what you want;
2. using '&' is a safe method. although it is rarely used, when it is used, the unset function should be called immediately after the reference is complete. otherwise, in the following code, if this variable is used again, the last value of the loop will be modified to get an unexpected value, for example: when the list is output cyclically, many messy values or null values will appear in the output of the last line. Unset can solve this problem.

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 ". $. ":". $ v."
";}?> // Run the 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 next array, and in most cases, it also works, but when the following array is an integer, such as array, 2, 3), the problem occurs. php performs intval on the previous string to get the value 0. if it happens that your array contains this value, then the equation is established, isn't it more than expected?
Therefore, when we determine that the subsequent data is an integer, especially 0 (this may replace all strings), you must never use this function any more, key_exists can be used, but the subsequent data must be reversed using array_flip.

Let's take a look at the example.

Function search ($ keyWord, $ stack) {// determine whether to update or insert foreach ($ stack as $ key => $ val) {if (in_array ($ keyWord, $ val) {return TRUE;} return FALSE ;}

What happens when a string is taken as an array to get a value? Php is a highly fault-tolerant language. it will try to correct errors for you, so it is very clever to convert your reference subscript to an integer, of course, it will get 0, if the subscript of a string is 0, the value of the first string will be obtained. Is it beyond your expectation? The solution is to check whether the variable is an array (is_array) before referencing the subscript.

Although many of our predecessors have encountered one or more problems, they are still talking about it, but we are still inevitably making mistakes. This may be society! Everyone is so busy, how can I crack your bad code! Haha

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.