php遍歷關聯陣列的三種方法

pengfoo發表於2014-03-20
<?php
    $sports1 = array('football' =>'good' ,
                    'basketball'=>'bad',
                    'volleyball'=>'well' );
    $sports2 = array('football' =>'good' ,
                    'basketball'=>'bad',
                    'volleyball'=>'well' );
    $sports3 = array('football' =>'good' ,
                    'basketball'=>'bad',
                    'volleyball'=>'well' );

    //method 1
    foreach ($sports1 as $key => $value) {
        echo $key.":".$value." <br />";
    }

    //method 2
    while ($element = each($sports2)) {
        echo $element['key'].": ".$element['value']."<br />";
    }

    //method 3
    while (list($key,$value) = each($sports3)) {
        echo $key.": ".$value."<br />";
    }
?>

相關文章