简体   繁体   中英

Build separate arrays from multidimensional array in 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>";

}
$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] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM