There are three common ways to traverse an array in PHP

The most manipulated data in PHP is estimated to be an array, which has the characteristics of high efficiency, fast speed and convenient storage.

There are three commonly used methods for traversing arrays in PHP:
1. For loop, the usage is the most flexible, flexible enough to doubt life, but remember the format is also very simple.
2. Foreach is a function specially provided by PHP for array traversal. It was introduced in the PHP4 version and has the highest execution efficiency
. 3. Combined use of list(), each() and while loop to traverse arrays. ) function is used a lot

Look directly at the example code:

<?php
$arr1 = array('http://www.jinsanguo.com/','Jin Sanguo','PHP Tutorial');
$num = count($arr);//count() is an array statistical function
	for($i=0;$i<$num;++$i){
            echo $arr[$i];
        }

$arr2 = array('http://www.jinsanguo.com/','Jin Sanguo','PHP Tutorial');
        foreach($arr2 as $value){
          echo $value;
        }

$arr3 = array('http://www.jinsanguo.com/','Jin Sanguo','PHP Tutorial');
      while(list($key,$value) = each($arr3)){
          echo $key.'=>'.$value;
	}
?>

 

 The each() function takes an array as a parameter, returns the key/value pair of the current element in the array, and moves the array pointer backwards to the position of the next element.

The list() function, which is not a real function, is a language construct of PHP. list() assigns a value to a set of variables in one operation.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326711049&siteId=291194637