dongliulu1122
2011-10-19 09:04
浏览 146
已采纳

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

The following is a 3-dimentional Array. I want every array like

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

to be stored into a $bookArray. I have used foreach loop. However, the output isn't correct.

Is there anyone can point out where error is? Thanks!

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

            }   
        }       
    ?>

图片转代码服务由CSDN问答提供 功能建议

以下是三维数组。 我希望每个数组都像

  array(“captureTime”=&gt;“2011-08-11 11:33:15”,
“channel1_Data”=&gt;“23.00  “,
”channel2_Data“=&gt;”53.55“)
   
 
 

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

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

 &lt;?php 
 $ Books = array(“0”=&gt; array(
 array(“captureTime”=&gt;“2011-08-  11 11:33:15“,”channel1_Data“=&gt;”23.00“,”channel2_Data“=&gt;”53.55“),
 array(”captureTime“=&gt;”2011-08-12 11:33:15  “,”channel1_Data“=&gt;”23.00“,”channel2_Data“=&gt;”53.55“),
 array(”captureTime“=&gt;”2011-08-13 11:33:15“,”channel1_Data“=  &gt;“23.00”,“channel2_Data”=&gt;“53.55”)
),
“1”=&gt;数组(
 array(“captureTime”=&gt;“2011-08-14 11:33:  15“,”channel1_Data“=&gt;”23.00“,”channel2_Data“=&gt;”53.55“)
),
”2“=&gt;数组(
 array(”captureTime“=&gt;”2011-  08-15 11:33:15“,”channel1_Data“=&gt;”23.00“,”channel2_Data“=&gt;”53.55“),
 array(”captureTime“=&gt;”2011-08-16 11:33  :1  5“,”channel1_Data“=&gt;”23.00“,”channel2_Data“=&gt;”53.55“)
),
); 
 
 
 $ bookArray = array(); 
 
 foreach(  $ Books as $ key =&gt; $ values){
 foreach($ values as $ subKey =&gt; $ subValue){
 $ bookArray = $ Books [$ key] [$ subKey]; 
 
} \  n} 
?&gt; 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doufang2023 2011-10-19 09:06
    已采纳

    It should be this instead:

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

    You forgot the [] append-to-array shorthand :)

    p/s: you can replace $Books[$key][$subKey] with $subValue within the loop:

    $bookArray[] = $subValue;
    
    点赞 评论

相关推荐 更多相似问题