The PHP foreach () function is limited to arrays using the _php tutorial

Source: Internet
Author: User
Tags php foreach

This is just a simple way to iterate through an array. The PHP foreach () function can only be used in arrays, and an error occurs when trying to use it for other data types or an uninitialized variable. There are two kinds of syntax, and the second is a useful extension that is minor but the first.

foreach (array_expression as $value)
Statement
foreach (array_expression as $key = $value)
Statement

The first format iterates through the given array of array_expression. In each loop, the value of the current cell is assigned to the $value and the pointer inside the array is moved forward one step (so the next cell in the next loop will be taken).

The second format does the same thing, except that the key name of the current cell is assigned to the variable $key in each loop.

It is possible to traverse objects since PHP 5.

Note: When the PHP foreach () function starts executing, the pointer inside the array automatically points to the first cell. This means that you do not need to call Reset () before the Foreach loop.

Note: Unless the array is referenced, the PHP foreach () function operates on a copy of the specified array, not the array itself. So the array pointers are not changed by the each () structure, and the changes to the returned array cells do not affect the original array. However, the internal pointer of the original array does move forward in the process of handling the array. Assuming that the Foreach loop runs to the end, the internal pointer to the original array will point to the end of the array.

Starting with PHP 5, it is easy to modify the cells of an array by adding & to it before $value. This method assigns a value to a reference instead of copying a value.

 
  
  
  1. < ? PHP
  2. $ arr Array(1, 2, 3, 4);
  3. foreach ($arr as & $value) {
  4. $ value = $value * 2;
  5. }
  6. $arr is now Array (2, 4, 6, 8)
  7. ?>

This method of the PHP foreach () function is only available if the array being traversed can be referenced (for example, a variable).


http://www.bkjia.com/PHPjc/446012.html www.bkjia.com true http://www.bkjia.com/PHPjc/446012.html techarticle This is just a simple way to iterate through an array. The PHP foreach () function can only be used in arrays, and an error occurs when trying to use it for other data types or an uninitialized variable. There are two kinds of ...

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