PHP中遍历数组的方法有以下几种: 使用foreach循环:可以使用foreach循环来遍历数组,语法如下: $colors=array("red",
PHP中遍历数组的方法有以下几种:
- 使用foreach循环:可以使用foreach循环来遍历数组,语法如下:
$colors=array("red","green","blue");
foreach($colorsas$color){
echo$color."<br>";
}
$colors=array("red","green","blue");
$length=count($colors);
for($i=0;$i<$length;$i++){
echo$colors[$i]."<br>";
}
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
reset($age);
while(list($key,$value)=each($age)){
echo$key."is".$value."yearsold<br>";
}
functionmyfunction($value,$key){
echo"$key:$value<br>";
}
$colors=array("red","green","blue");
array_walk($colors,"myfunction");
functionmyfunction($value){
return$value*$value;
}
$numbers=array(1,2,3,4,5);
$new_numbers=array_map("myfunction",$numbers);
print_r($new_numbers);
版权声明
本文仅代表作者观点,不代表博信信息网立场。