Home >Backend Development >PHP Tutorial >Creation and traversal of arrays in php
The establishment of arrays in php can be divided into 3 types of methods. When traversing the array, we will often use the count($arr) function. This function returns the length of the array:
1.
//数组创建的第一种方法 $arr[0] = 11; $arr[1] = "ee"; $arr[2] = 11.1; $arr[3] = true; $arr[4] = null; for($i = 0; $i < count($arr); $i++) { echo '<br/>'.$arr[$i]; }
//数组创建的第二种方法 $arr = array(1,"ee",true); for($i = 0; $i < count($arr); $i++) { echo '<br/>'.$arr[$i]; }
//数组创建的第三种方法(以键值对的方式进行存储) $arr['name'] = "raid"; $arr['pwd'] = "eee"; foreach($arr as $key=>$val) { echo $key."=".$val."<br/>"; }
$arr = array(5=>"logo","ee"=>12,"ff"=>13); //显示整个数组的情况 print_r($arr); echo "<br/>"; var_dump($arr); echo "<br/>".is_array($arr);
$str = "e a d d"; $arr = explode(" ", $str); foreach($arr as $key=>$val) { echo "<br/>".$key."=".$val; }
Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.
The above introduces the creation and traversal of arrays in PHP, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.