Summary of 4 Methods of php Traversing Array

  • 2021-07-06 10:34:50
  • OfStack

In php can be used to traverse the array of many functions, such as: for statement, list, each, foreach these four functions, which is also in php traversal of the array of several major functions, I will give you an introduction below.

foreach Traversal Array

When we use arrays, we often want to traverse the array and get each key or element value. php provides a special function to traverse arrays. Here, we first introduce the usage of foreach traversal array function.

Structural form:


foreach ( array_expression as $value ) statement
/* array_expression Is the array to traverse
   as Assigns the value of the array to the $value
   statement Is a subsequent statement
*/

Example 1:


 ' White ' ,
  'black' => ' Black ' ,
  'red' => ' Red ' ,
  'green' => ' Green ',
  'yellow' => ' Yellow ');
 foreach( $color as $c) echo $c ."
";   
?>

Through foreach, you can not only get the value of the element, but also get the key name and structure form:

foreach ( array_expression as $key => $value ) statement

Add the code in line 7 of the above example:
foreach( $color as $c) echo $c ."<br>";

Replace with the following:
foreach( $color as $key => $c) echo $key.$c ."<br>";

each Traversal Array

Traversing an array is an important part of php array operation. In addition to the foreach function mentioned earlier, we will introduce another function of traversing an array-each ().

The each () function can be used to output the key name of the current pointer position and the corresponding element value. You can use "0" or "key" to access the key name (identifier) and "1" or "value" to access the value corresponding to the identifier.

Example:


<?php
 $languages=array(1=-->"php",
  5=>"html",
  10=>"css");
 $a=each($languages); /* No. 1 1 Sub-traversal array */ 
 echo $a[0] ."t";
 echo $a[1] ."<br>";
 $a=each($languages); /* No. 1 2 Sub-traversal array */
 echo $a[key] ."t";
 echo $a[value];   
?>

list Traversal Array

The function list can be assigned to a variable once while traversing an array, and is usually used in conjunction with the function each (). The list () function makes it easier to access the keys and values returned by each ().

Example:


<?php
 $date=array(1=-->"Monday",
  2=>"Tuesday",
  3=>"Wednesday");
 list($key,$value)=each($date); /* Ergodic function */
 echo "$key $value" ."<br>"; /* Output number 1 Array of numbers */
 $next=next($date);  /* Pointer moves backward */
 echo "$next"; 
?>

ps: The list () function is just the opposite of the array () function, which constructs a series of data into an array, while list () splits the array into data.

for Traversal Array

In addition to the php predefined functions for traversing arrays, we can also use the loop characteristics of for statements to traverse arrays. Here's an example:


<?php
 $a[]=" Jacky Cheung "; /* Defining Array */
 $a[]=" Andy Lau ";
 $a[]=" Leon Lai Ming ";
 $a[]=" Aaron Kwok ";
 $s=count($a); /* Count the number of arrays */
 for($i=0;$i<$s;$i++){ /* Traversing an array */
  echo $a[$i] ."<br /-->"; /* Display array */
 }
?>


Related articles: