PHP - 如何在foreach数组时维护数组的顺序?

时间:2014-06-19 15:12:08

标签: php arrays foreach

我在处理foreach时遇到维护数组顺序的问题。

原始数组是变量$status

Array ( 
    [0] => Array ( [id] => 11 [name] => name 11 [order] => 1 ) 
    [1] => Array ( [id] => 4 [name] => name 4 [order] => 2 ) 
    [2] => Array ( [id] => 12 [name] => name 12 [order] => 3 ) 
    [3] => Array ( [id] => 5 [name] => name 5 [order] => 4 ) 
    [4] => Array ( [id] => 6 [name] => name 6 [order] => 5 ) 
    [5] => Array ( [id] => 7 [name] => name 7 [order] => 6 ) 
    [6] => Array ( [id] => 9 [name] => name 9 [order] => 7 ) 
    [7] => Array ( [id] => 8 [name] => name 8 [order] => 8 ) 
    [8] => Array ( [id] => 10 [name] => name 10 [order] => 9 ) 
    [9] => Array ( [id] => 1 [name] => name 1 [order] => 10 ) 
    [10] => Array ( [id] => 2 [name] => name 2 [order] => 11 ) 
    [11] => Array ( [id] => 3 [name] => name 3 [order] => 12 ) 
    [12] => Array ( [id] => 13 [name] => name 13 [order] => 13 ) 
)

现在,当我联系这个数组时:

    foreach ($status as $item)
    {
        if(!isset($report['status'][$item['id']]))
        {
            $report['status'][$item['id']] = Array();
        }
    }

我明白了:

Array (  
    [status] => Array ( 
                [1] => Array ( 
                        [2014-05-02] => 2 
                        [2014-05-05] => 2 
                        [2014-05-06] => 1 
                        [2014-05-07] => 1 
                        [2014-05-08] => 5 ) 
                [4] => Array ( 
                        [2014-05-02] => 3 
                        [2014-05-05] => 147 
                        [2014-05-06] => 10 
                        [2014-05-07] => 9 
                        [2014-05-08] => 7 ) 
                [5] => Array ( 
                        [2014-05-02] => 2 
                        [2014-05-05] => 20 
                        [2014-05-06] => 2 
                        [2014-05-07] => 1 
                        [2014-05-08] => 10 ) 
                [6] => Array ( 
                        [2014-05-02] => 1 
                        [2014-05-05] => 2 
                        [2014-05-06] => 2 
                        [2014-05-08] => 2 ) 
                [7] => Array (  
                        [2014-05-02] => 2 
                        [2014-05-05] => 14 
                        [2014-05-06] => 1 
                        [2014-05-07] => 1 
                        [2014-05-08] => 4 ) 
                [8] => Array ( 
                        [2014-05-02] => 1 
                        [2014-05-05] => 8 
                        [2014-05-08] => 1 ) 
                [9] => Array ( 
                        [2014-05-02] => 2 
                        [2014-05-05] => 8 
                        [2014-05-06] => 1 
                        [2014-05-07] => 1 
                        [2014-05-08] => 4 ) 
                [10] => Array ( 
                        [2014-05-02] => 2 
                        [2014-05-05] => 8 
                        [2014-05-06] => 1 
                        [2014-05-07] => 1 
                        [2014-05-08] => 1 ) 
                [11] => Array ( ) 
                [12] => Array ( ) 
                [2] => Array ( ) 
                [3] => Array ( ) 
                [13] => Array ( ) 
    ) 
)

但我需要的是:

Array (  
    [status] => Array (
                [11] => Array ( ) 
                [4] => Array ( 
                        [2014-05-02] => 3 
                        [2014-05-05] => 147 
                        [2014-05-06] => 10 
                        [2014-05-07] => 9 
                        [2014-05-08] => 7 )
                [12] => Array ( ) 
                [5] => Array ( 
                        [2014-05-02] => 2 
                        [2014-05-05] => 20 
                        [2014-05-06] => 2 
                        [2014-05-07] => 1 
                        [2014-05-08] => 10 )
                [6] => Array ( 
                        [2014-05-02] => 1 
                        [2014-05-05] => 2 
                        [2014-05-06] => 2 
                        [2014-05-08] => 2 ) 
                [7] => Array (  
                        [2014-05-02] => 2 
                        [2014-05-05] => 14 
                        [2014-05-06] => 1 
                        [2014-05-07] => 1 
                        [2014-05-08] => 4 ) 
                [9] => Array ( 
                        [2014-05-02] => 2 
                        [2014-05-05] => 8 
                        [2014-05-06] => 1 
                        [2014-05-07] => 1 
                        [2014-05-08] => 4 ) 
                [8] => Array ( 
                        [2014-05-02] => 1 
                        [2014-05-05] => 8 
                        [2014-05-08] => 1 ) 
                [10] => Array ( 
                        [2014-05-02] => 2 
                        [2014-05-05] => 8 
                        [2014-05-06] => 1 
                        [2014-05-07] => 1 
                        [2014-05-08] => 1 ) 
                [1] => Array ( 
                        [2014-05-02] => 2 
                        [2014-05-05] => 2 
                        [2014-05-06] => 1 
                        [2014-05-07] => 1 
                        [2014-05-08] => 5 )  
                [2] => Array ( ) 
                [3] => Array ( ) 
                [13] => Array ( ) 
    ) 
)

所以,我的问题是,如何维护原始数组$ status的顺序?

最诚挚的问候,

1 个答案:

答案 0 :(得分:-1)

为什么不保留第一个索引,这应该更容易

foreach ($status as $key => $item)
    {
        if(!isset($report['status'][$key][$item['id']]))
        {
            $report['status'][$key][$item['id']] = Array();
        }
    }

您确实有一个额外的图层,但如果需要,可以忽略它。