Summary of Several Common Ways of PHP Array Traversal
- 2021-11-24 01:06:58
- OfStack
In this paper, several common ways of PHP array traversal are described with examples. Share it for your reference, as follows:
1. Use the for loop to iterate through an array
conut($arr);
Used to count the number of array elements.
for loop can only be used for traversal, pure index array! ! ! !
If there are associative arrays, count statistics will count the total number of two arrays, using for loop traversal of mixed arrays, resulting in arrays out of bounds! !
eg:
$arr = array(1,2,3,5,6,7);
$num = count($arr); //count Better put it for Outside, you can let the function only execute 1 Times
echo " Number of array elements {$num}<br/>";
for($i=0;$i<$num;$i++){
echo "{$i}==>{$arr[$i]}<br/>";
}
2. forEach loops through arrays
foreach can traverse any type of array! ! !
eg:
$arr = array(1,2,3,"one"=>4,5,6,7);
foreach($arr as $value){
echo "{$item}<br>";
}
foreach($arr as $key => $value){
echo "{$key}==>{$item}<br>";
}
For example, parse the following array:
$h51701 = array(
"group1"=>array(
array("name"=>" Zhang 3","age"=>14,"sex"=>" Male "),
array("name"=>" Zhang 3","age"=>14,"sex"=>" Male "),
array("name"=>" Zhang 3","age"=>14,"sex"=>" Male ")
),
"group2"=>array(
array("name"=>" Zhang 3","age"=>14,"sex"=>" Male "),
array("name"=>" Zhang 3","age"=>14,"sex"=>" Male "),
array("name"=>" Zhang 3","age"=>14,"sex"=>" Male ")
),
"group3"=>array(
array("name"=>" Zhang 3","age"=>14,"sex"=>" Male "),
array("name"=>" Zhang 3","age"=>14,"sex"=>" Male "),
array("name"=>" Zhang 3","age"=>14,"sex"=>" Male ")
)
);
foreach ($h51701 as $key => $value) {
echo "{$key}<br><br>";
foreach ($value as $key1 => $value1) {
echo " No. 1 ".($key1+1)." A classmate <br>";
foreach ($value1 as $key2 => $value2) {
echo "{$key2}==>{$value2}<br>";
}
echo "<br>";
}
echo "------------------------<br>";
}
Traversing an array using list (), each (), while ()
list (): Used to assign every 1 value of an array to every 1 parameter of the list function. An argument to the list function, which must be less than or equal to the number of elements in the array;
eg:
list($a,$b,$c)=[1,2,3];
--
>
$a=1; $b=2; $c=3;
Note:
①
list()
When parsing arrays, only indexed arrays are parsed;
② list can selectively resolve the value of the array through null parameters;
list($a,,$b)=[1,2,3];
--
>
$a=1; $b=3;
(Focus)
each()
Used to return the key-value pair of the current pointer in the array! And move the pointer one bit backward;
Return value: If the pointer has the next 1 bit, it returns 1 array. Contains an index array (0-key, 1-value) and an associative array ("key"-key, "value"-value); If the pointer does not have the next 1 bit, return false;;
eg:
①
each($arr)
Returns an array or false;
Assign the array or false to $a;
③ while judges that if $a is an array, continue to execute for the next time;
If $a is false, terminate the loop
while($a = each($arr)){
echo "{$a[0]}-->{$a[1]}<br>";
echo "{$a['key']}-->{$a['value']}<br>";
}
3. Traverse the array using list ()/each ()/while ()
while(list($key,$value) = each($arr)){
echo "{$key}-->{$value}<br>";
}
reset($arr);
! ! ! ! Array usage
each()
After traversing once, the pointer uses the next 1 bit of the last 1 bit; Instant reuse
each()
false is always returned;
If you still need to use it, you need to use it
reset($arr);
Function to reset the array pointer;
eg:
$arr = array(1,2,3,"one"=>4,5,6,7);
foreach($arr as $value){
echo "{$item}<br>";
}
foreach($arr as $key => $value){
echo "{$key}==>{$item}<br>";
}
while(true){
$a = each($arr);
if($a){
echo "{$a[0]}-->{$a[1]}<br>";
echo "{$a['key']}-->{$a['value']}<br>";
}else{
break;
}
}
while(list($key,$value) = each($arr)){
echo "{$key}-->{$value}<br>";
}
reset($arr);
while(list($key,$value) = each($arr)){
echo "{$key}-->{$value}<br>";
}
4. Traverse an array using an array pointer
① next: Move the array pointer backward by 1 bit. And returns the value of the last 1 bit; false not returned
② prev: Move the array pointer forward by 1 bit. And returns the value of the first 1 bits; false not returned
③ end: Move the array pointer to the last 1 bit and return the value of the last 1 bit; Empty array returns false
④ reset: Restore the array pointer to the first bit. And returns the value of the first bit; Empty array returns false
⑤ key: Return the key where the current pointer is located;
⑥ current: Returns the value of the position where the current pointer is located;
$arr = [1,2,3,4,"one"=>5];
while(true){
echo key($arr);
echo "--";
echo current($arr);
echo "<br>";
if(!next($arr)){
break;
}
}
reset($arr);
The second way:
do{
echo key($arr);
echo "--";
echo current($arr);
echo "<br>";
}while(next($arr));
reset($arr);
A master hand's first small display:
1. Traverse the array:
$subject1 = array("Linux","PHP","MySQL","HTML","CSS","JQuery")
Mode 1: for loop traversal
for($i=0;$i<count($subject1);$i++){
echo $subject1[$i]."<br/>";
}
Mode 2: Use
list()
/
each()
/
while()
Cooperate with traversal array
Mode 1.
$arr = array(1,2,3,"one"=>4,5,6,7);
foreach($arr as $value){
echo "{$item}<br>";
}
foreach($arr as $key => $value){
echo "{$key}==>{$item}<br>";
}
0
Mode 2.
$arr = array(1,2,3,"one"=>4,5,6,7);
foreach($arr as $value){
echo "{$item}<br>";
}
foreach($arr as $key => $value){
echo "{$key}==>{$item}<br>";
}
1
Mode 3: forEach loop traversal
$arr = array(1,2,3,"one"=>4,5,6,7);
foreach($arr as $value){
echo "{$item}<br>";
}
foreach($arr as $key => $value){
echo "{$key}==>{$item}<br>";
}
2
Mode 4: Pointer
do{
echo key($subject1)."=>".current($subject1)."<br/>";
}while(next($subject1));
echo "<br/>";
2. Use reset (), end (), prev (), next (), key (), current (); Combine with do... while to output the values in the array backwards:
$arr = array(1,2,3,"one"=>4,5,6,7);
foreach($arr as $value){
echo "{$item}<br>";
}
foreach($arr as $key => $value){
echo "{$key}==>{$item}<br>";
}
4
For more readers interested in PHP related contents, please check the special topics of this site: "PHP Array (Array) Operation Skills Complete Book", "PHP Common Traversal Algorithms and Skills Summary", "PHP Data Structure and Algorithm Tutorial", "php Programming Algorithms Summary", "PHP Mathematical Operation Skills Summary", "php String (string) Usage Summary" and "php Common Database Operation Skills Summary"
I hope this article is helpful to everyone's PHP programming.