Home > Backend Development > PHP Tutorial > PHP traverses array list foreach each method summary_PHP tutorial

PHP traverses array list foreach each method summary_PHP tutorial

WBOY
Release: 2016-07-13 17:15:28
Original
1202 people have browsed it

There are many functions that can be used to traverse arrays in PHP. For example, there are four functions: for statement, list, each, and foreach. These are also the main functions for traversing arrays in PHP. Let me give them below. Introduce everyone.

foreach traverses the array

When we use arrays, we often need to traverse the array and obtain each key or element value. PHP provides some functions specifically for traversing arrays. Here we first introduce the usage of foreach array traversal function.

Structural form:

foreach (array_expression as $value) statement
/*array_expression is the array to be traversed
The function of as is to assign the value of the array to $value
Statement is the subsequent statement
*/
Example 1:

The code is as follows Copy code
 代码如下 复制代码

$color=array('white' => '白色' ,
       'black' => '黑色' ,
       'red' => '红色' ,
       'green' => '绿色',
       'yellow' => '黄色');
 foreach( $color as $c) echo $c ."
";   
?>

$color=array('white' => 'white' ,
'black' => 'black' ,

'red' => 'red' ,

'green' => 'green',
        'yellow' => 'yellow');

foreach( $color as $c) echo $c ."
";
 代码如下 复制代码


foreach( $color as $c) echo $c ."
";

改为:

foreach( $color as $key => $c) echo $key.$c ."
";

?>


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


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

Change the code on line 7 in the above example:

The code is as follows Copy code

foreach( $color as $c) echo $c ."
";

 代码如下 复制代码

$languages=array(1=>"php",
    5=>"html",
    10=>"css");
 $a=each($languages); /* 第一次遍历数组 */ 
 echo $a[0] ."t";
 echo $a[1] ."
"; 
 $a=each($languages); /* 第二次遍历数组 */
 echo $a[key] ."t";
 echo $a[value];   
?>

changed to: foreach( $color as $key => $c) echo $key.$c ."
";
each traverses the array Traversing an array is an important part of PHP array operations. In addition to the foreach function mentioned above, here is another function for traversing an array -each(). Use each() function to output the key name and corresponding element value of the current pointer position. You can use "0" or "key" to access the key name (identifier), and use "1" or "value" to access the value corresponding to the identifier. Example:
The code is as follows Copy code
$languages=array(1=>"php", 5=>"html", 10=>"css"); $a=each($languages); /* First traversal of array */ echo $a[0] ."t"; echo $a[1] ."
"; $a=each($languages); /* Traverse the array for the second time */ echo $a[key] ."t"; echo $a[value]; ?>

list traverses array

The function list can be assigned to variables once when traversing the array, and is usually used in conjunction with the each() function. Using the list() function makes it easier to access the keys and values ​​returned by each().

Example:

 代码如下 复制代码
$date=array(1=>"Monday",
      2=>"Tuesday",
      3=>"Wednesday");
 list($key,$value)=each($date); /* 遍历函数 */
 echo "$key $value" ."
"; /* 输出第一个数组 */
 $next=next($date);  /* 指针后移 */
 echo "$next";  
?>


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

for traverses the array

In addition to some of the predefined array traversal functions in PHP, we can also use the loop feature of the for statement to traverse the array and output it. Examples are given below:

The code is as follows
 代码如下 复制代码

 $a[]="张学友"; /* 定义数组 */
 $a[]="刘德华";
 $a[]="黎明";
 $a[]="郭富城";
 $s=count($a); /* 统计数组个数 */
 for($i=0;$i<$s;$i++){ /* 遍历数组 */
  echo $a[$i] ."
"; /* 显示数组 */
 }
?>

Copy code
$a[]="Andy Lau";

$a[]="Dawn"; $s=count($a); /* Count the number of arrays */ for($i=0;$i<$s;$i++){ /* Traverse the array */ echo $a[$i] ."
"; /* Display array */ } ?>
http://www.bkjia.com/PHPjc/628839.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628839.htmlTechArticleThere are many functions that can be used to traverse arrays in php, such as: for statement, list, each, foreach These four functions are also the main functions for traversing arrays in php. Let me do it next...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template