php遍历关联数组

$arr=['red'=>'苹果','orange'=>'橙子','green'=>'西瓜'];

key():是当前数组指针的键名,current()当前数组指针的值,next()将数组指针往后移一个坐标

 //for循环遍历

for($i=0;$i<count($arr);$i++){

        echo key($arr).'=>'.current($arr).'<br>';

        next($arr);

    };

//while遍历

$i=0;

while($i<count($arr)){
    echo key($arr).'=>'.current($arr).'<br>';
    next($arr);
    $i++;
}

//foreach遍历

foreach ($arr as $key => $value) {
    echo $key.'=>'.$value;
}

猜你喜欢

转载自blog.csdn.net/weixin_42881256/article/details/82802151