PHP Traversal Array Common way (for,foreach,while, pointers, etc.)

Source: Internet
Author: User

1 iterating through an array with a For loop

Count ($arr) to count the number of array elements
The For loop can only be used for traversal, pure indexed arrays!! If an associative array exists, count counts the total number of two arrays
Traversing a mixed array with a For loop, resulting in an array out of bounds


$arr =array (1,2,3,4,5,6,7);
$num =count ($arr);//count better put it outside for the function to perform only once
for ($i =0; $i <count ($arr); $i + +) {
echo "{$i}==>{$arr [$i]}<br/>];
}

2.foreach Loop traversal Array (foreach can facilitate any type of array)

For example:

$arr =array (1,2,3,5,6,7, "one" =>9);
foreach ($arr as $item) {

echo "{$item}<br/>";
}
foreach ($arr as $key = = $item) {

echo "{$key}==>{$item}<br/>";
}

3.3. Use list (), each (), and while () to iterate through the array

Usage:

while (list ($key, $value) =each ($arr)) {

echo "{$key}-->{$value}<br/>";
}

List (): Used to assign each value of the array to each parameter of the list function. (The parameter of the list function must be less than or equal to the number of array elements)

Eg:list ($a, $b, $c) =[1,2,3];--> $a =1; $b =2; $c = 3;
Note: ①list parses an array when the index array is parsed directly
②list can selectively parse the value of an array by passing null arguments
List ($a, $b) =[1,2,3];--> $a =1; $b = 3;
Each (): Used to return the key-value pair in which the current pointer is in the array! And move the pointer back one bit;
Return value: Returns an array if the array has the next one. Contains an indexed array (0-key, 1-key) and an associative array ("Key"-Key, "value"-value);
If the pointer does not have the next one return false;

The pointer uses the next bit in the last digit after the ③ array is traversed using each (), and then each () always returns false
If you want to use Reset ($arr)

4. Traversing functions with array pointers
①next: Moves the array pointer back one bit. and returns the value of the next bit; no return false;
②prev: Moves the array pointer forward one bit. and returns the value of the next bit; no return false;
③end: Moves the array pointer to the last one. and returns the value of the next bit; the empty array returns false;
④reset: Returns the array pointer to the first bit. and returns the value of the first bit; the empty array returns false;
⑤key: Returns the key that the current pointer is in;
⑥current: Returns the value in which the current pointer is in place;

PHP Traversal Array Common way (for,foreach,while, pointers, etc.)

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.