dongxibeng5324 2015-02-23 20:47
浏览 39
已采纳

在php中从多维数组构建单独的数组

I have a multidimensional array $output

Array 
( 
    [2015-02-23] => 
        Array 
        ( 
            [0] => Array ( [timestamp] => 2015-02-23T16:00:00 [temp] => -7 [time] => 16) 
            [1] => Array ( [timestamp] => 2015-02-23T19:00:00 [temp] => -13 [time] => 19) 
            [2] => Array ( [timestamp] => 2015-02-23T22:00:00 [temp] => -16 [time] => 22)
        ) 
    [2015-02-24] => 
        Array 
        ( 
            [0] => Array ( [timestamp] => 2015-02-24T01:00:00 [temp] => -18 [time] => 01) 
            [1] => Array ( [timestamp] => 2015-02-24T04:00:00 [temp] => -19 [time] => 04) 
            [2] => Array ( [timestamp] => 2015-02-24T07:00:00 [temp] => -19 [time] => 07) 
            [3] => Array ( [timestamp] => 2015-02-24T10:00:00 [temp] => -14 [time] => 10) 
            [4] => Array ( [timestamp] => 2015-02-24T13:00:00 [temp] => -10 [time] => 13) 
            [5] => Array ( [timestamp] => 2015-02-24T16:00:00 [temp] => -8 [time] => 16) 
            [6] => Array ( [timestamp] => 2015-02-24T19:00:00 [temp] => -10 [time] => 19)
            [7] => Array ( [timestamp] => 2015-02-24T22:00:00 [temp] => -8 [time] => 22) 
        ) 
    [2015-02-25] => 
        Array 
        ( 
            [0] => Array ( [timestamp] => 2015-02-25T01:00:00 [temp] => -6 [time] => 01) 
        ) 
)

How can I create new arrays from the subarrays? So that

$output_0:

Array 
    ( 
        [0] => Array ( [timestamp] => 2015-02-23T16:00:00 [temp] => -7 [time] => 16) 
        [1] => Array ( [timestamp] => 2015-02-23T19:00:00 [temp] => -13 [time] => 19) 
        [2] => Array ( [timestamp] => 2015-02-23T22:00:00 [temp] => -16 [time] => 22)
    ) 

$output_1:

Array 
    ( 
        [0] => Array ( [timestamp] => 2015-02-24T01:00:00 [temp] => -18 [time] => 01) 
        [1] => Array ( [timestamp] => 2015-02-24T04:00:00 [temp] => -19 [time] => 04) 
        [2] => Array ( [timestamp] => 2015-02-24T07:00:00 [temp] => -19 [time] => 07) 
        [3] => Array ( [timestamp] => 2015-02-24T10:00:00 [temp] => -14 [time] => 10) 
        [4] => Array ( [timestamp] => 2015-02-24T13:00:00 [temp] => -10 [time] => 13) 
        [5] => Array ( [timestamp] => 2015-02-24T16:00:00 [temp] => -8 [time] => 16) 
        [6] => Array ( [timestamp] => 2015-02-24T19:00:00 [temp] => -10 [time] => 19)
        [7] => Array ( [timestamp] => 2015-02-24T22:00:00 [temp] => -8 [time] => 22) 
    ) 

$output_2:

Array
    ( 
        [0] => Array ( [timestamp] => 2015-02-25T01:00:00 [temp] => -6 [time] => 01) 
    )

I have began with a recursive iterator but can't find the logic in how to build the new arrays from the values gathered with

$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($output));
foreach ($iterator as $value) {

    print strtoupper($value) . " <br>";

}
  • 写回答

1条回答 默认 最新

  • doudichu1358 2015-02-23 20:56
    关注
    $output_0 = $output['2015-02-23'];
    $output_1 = $output['2015-02-24'];
    $output_2 = $output['2015-02-25'];
    

    But I don't understand what is point of doing that.

    You can also use $output = array_values($output);. That gives you $output[0], $output[1] and $output[2].

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里