将三维数组转换为二维数组

时间:2011-10-19 09:04:54

标签: php multidimensional-array

以下是三维阵列。我希望每个阵列都像

array("captureTime"=>"2011-08-11 11:33:15", 
      "channel1_Data"=>"23.00", 
      "channel2_Data"=>"53.55")

存储在 $ bookArray 中。我用过foreach循环。但是,输出不正确。

是否有人可以指出错误在哪里?谢谢!

<?php
        $Books =array("0"=>array(
                                 array("captureTime"=>"2011-08-11 11:33:15", "channel1_Data"=>"23.00", "channel2_Data"=>"53.55"), 
                                 array("captureTime"=>"2011-08-12 11:33:15", "channel1_Data"=>"23.00", "channel2_Data"=>"53.55"),
                                 array("captureTime"=>"2011-08-13 11:33:15", "channel1_Data"=>"23.00", "channel2_Data"=>"53.55")
                                 ),
                      "1"=>array(
                                 array("captureTime"=>"2011-08-14 11:33:15", "channel1_Data"=>"23.00", "channel2_Data"=>"53.55")
                                 ),
                      "2"=>array(
                                 array("captureTime"=>"2011-08-15 11:33:15", "channel1_Data"=>"23.00", "channel2_Data"=>"53.55"), 
                                 array("captureTime"=>"2011-08-16 11:33:15", "channel1_Data"=>"23.00", "channel2_Data"=>"53.55")
                                 ),
                      );


        $bookArray = array();

        foreach($Books as $key=>$values){
            foreach($values as $subKey=>$subValue){
                $bookArray = $Books[$key][$subKey];

            }   
        }       
    ?>

1 个答案:

答案 0 :(得分:2)

应该是这样:

$bookArray[] = $Books[$key][$subKey];

您忘记了[]追加到数组的简写:)

p / s:您可以在循环中将$Books[$key][$subKey]替换为$subValue

$bookArray[] = $subValue;