包含数组和非数组元素的多维数组,在php中遍历



如何遍历如下结构:

    $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
                                                     )                                                                
                         );  

假设你想要转弯…

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

你可以使用简单的递归:

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]);

相关内容

  • 没有找到相关文章

最新更新