具有数组和非数组作为元素的多维数组,在php中遍历

时间:2013-04-16 12:00:23

标签: php multidimensional-array

我怎样才能遍历如下的结构:

    $this->user[$userid] =  array(
                                 "initial" => array(
                                              "amount" =>$amount,
                                              "cards" =>$cards
                                              ),
                                 "userturn" => array(
                                               "userturn1" => array(
                                                              "action"=>$action,
                                                              "amount"=>$amount,
                                                              "date"=>$datetime 
                                                              ),
                                                "userturn2" => array(
                                                              "action"=>$action,
                                                              "amount"=>$amount,
                                                              "date"=>$datetime 
                                                              ),
                                                              .
                                                              .
                                                              .
                                                              .
                                                              n times
                                                     )                                                                

                         );  

2 个答案:

答案 0 :(得分:1)

假设你想转弯..

foreach($this->user[$userid]['userturn'] as $k=>$turn){
    print_r($turn);
}

答案 1 :(得分:0)

你可以使用简单的递归:

function recurseLoop($arr){
    foreach($arr as $key=>$value){
         echo('key:' . $key);
         if(gettype($value) == 'array'){
             recurseLoop($value);
         }
         else{
            echo('value:' . $value);
         }

    }
}

recurseLoop($this->user[$userid]);