Home > Article > Backend Development > Array traversal loop implementation program in php_PHP tutorial
The most commonly used methods for array traversal in PHP are forac, while, and for. Let’s introduce the implementation codes of these three array traversals.
People often ask me, if a PHP array is accessed using foreach, is the order of traversal fixed? In what order should it be traversed?
For example:
The code is as follows
|
Copy code
|
||||
代码如下 | 复制代码 | ||||
$arr[2] = 'huixinchen'; |
$arr['baidu'] = 2008; foreach ($arr as $key => $val)
{//What is the result?
}代码如下 | 复制代码 |
$arr = array(1,2,3,4,5); {//获取不到} |
Another example:
The code is as follows |
Copy code |
代码如下 | 复制代码 |
for($i=0,$l=count($arr); $i |
$arr[1] = 2007;$arr[0] = 2008;
foreach ($arr as $key => $val){
//What is the result now? }