从两个数组创建新数组

时间:2012-05-03 07:48:22

标签: php arrays variables sum

好的,所以我有两个数组

Array ( 
[0] => 23225 
[1] => 23224 
[2] => 23212 
[3] => 23188 
[4] => 23048 
[5] => 23027 
[6] => 23024 
[7] => 23018 
[8] => 22994 
[9] => 22992 )

Array ( 
[0] => Array ( [id] => 2108 [person] => [time_id] => 9 [version_id] => 22992 [day] => 21 ) 
[1] => Array ( [id] => 2168 [person] => [time_id] => 9 [version_id] => 22994 [day] => 2 ) 
[2] => Array ( [id] => 2169 [person] => [time_id] => 9 [version_id] => 22994 [day] => 3 ) 
[3] => Array ( [id] => 2170 [person] => [time_id] => 9 [version_id] => 22994 [day] => 4 ) 
[4] => Array ( [id] => 2171 [person] => [time_id] => 9 [version_id] => 22994 [day] => 5 ) 
[5] => Array ( [id] => 2172 [person] => [time_id] => 9 [version_id] => 22994 [day] => 6 ) 
[6] => Array ( [id] => 2173 [person] => [time_id] => 16- [version_id] => 22994 [day] => 10 ) 
[7] => Array ( [id] => 2174 [person] => [time_id] => 16- [version_id] => 22994 [day] => 11 ) 
[8] => Array ( [id] => 2175 [person] => [time_id] => 16- [version_id] => 22994 [day] => 12 ) 
[9] => Array ( [id] => 2176 [person] => [time_id] => 16- [version_id] => 22994 [day] => 13 ) 
[10] => Array ( [id] => 2177 [person] => [time_id] => 9 [version_id] => 22994 [day] => 16 ) 
[11] => Array ( [id] => 2178 [person] => [time_id] => 9 [version_id] => 22994 [day] => 17 ) 
[12] => Array ( [id] => 2179 [person] => [time_id] => 9 [version_id] => 22994 [day] => 18 ) 
[13] => Array ( [id] => 2180 [person] => [time_id] => 9 [version_id] => 22994 [day] => 19 ) 
[14] => Array ( [id] => 2181 [person] => [time_id] => 9 [version_id] => 22994 [day] => 20 ) 
[15] => Array ( [id] => 2182 [person] => [time_id] => 16- [version_id] => 22994 [day] => 23 ) 
[16] => Array ( [id] => 2183 [person] => [time_id] => 16- [version_id] => 22994 [day] => 24 ) 
[17] => Array ( [id] => 2184 [person] => [time_id] => 16- [version_id] => 22994 [day] => 25 ) 
[18] => Array ( [id] => 2185 [person] => [time_id] => 16- [version_id] => 22994 [day] => 26 ) 
[19] => Array ( [id] => 2186 [person] => [time_id] => 16- [version_id] => 22994 [day] => 27 ) 
[20] => Array ( [id] => 2187 [person] => [time_id] => 9 [version_id] => 22994 [day] => 30 ) 
[21] => Array ( [id] => 2386 [person] => [time_id] => 9 [version_id] => 23018 [day] => 19 ) 
[22] => Array ( [id] => 2483 [person] => [time_id] => 10-- [version_id] => 23024 [day] => 2 ) 
[23] => Array ( [id] => 2484 [person] => [time_id] => 10-- [version_id] => 23024 [day] => 3 ) 
[24] => Array ( [id] => 2485 [person] => [time_id] => 10-- [version_id] => 23024 [day] => 4 ) 
[25] => Array ( [id] => 2486 [person] => [time_id] => 10-- [version_id] => 23024 [day] => 5 ) 
[26] => Array ( [id] => 2487 [person] => [time_id] => 10-- [version_id] => 23024 [day] => 6 ) 
[27] => Array ( [id] => 2592 [person] => [time_id] => 15-- [version_id] => 23027 [day] => 10 ) 
[28] => Array ( [id] => 2593 [person] => [time_id] => 15-- [version_id] => 23027 [day] => 11 ) 
[29] => Array ( [id] => 2594 [person] => [time_id] => 15-- [version_id] => 23027 [day] => 12 ) 
[30] => Array ( [id] => 2595 [person] => [time_id] => 15-- [version_id] => 23027 [day] => 13 ) 
[31] => Array ( [id] => 2877 [person] => [time_id] => 17- [version_id] => 23048 [day] => 23 ) 
[32] => Array ( [id] => 2878 [person] => [time_id] => 17- [version_id] => 23048 [day] => 24 ) 
[33] => Array ( [id] => 2879 [person] => [time_id] => 17- [version_id] => 23048 [day] => 25 ) 
[34] => Array ( [id] => 2880 [person] => [time_id] => 17- [version_id] => 23048 [day] => 26 ) 
[35] => Array ( [id] => 2881 [person] => [time_id] => 17- [version_id] => 23048 [day] => 27 ) 
[36] => Array ( [id] => 3487 [person] => [time_id] => 9 [version_id] => 23188 [day] => 2 ) 
[37] => Array ( [id] => 3488 [person] => [time_id] => 9 [version_id] => 23188 [day] => 3 ) 
[38] => Array ( [id] => 3489 [person] => [time_id] => 9 [version_id] => 23188 [day] => 4 ) 
[39] => Array ( [id] => 3490 [person] => [time_id] => 9 [version_id] => 23188 [day] => 5 ) 
[40] => Array ( [id] => 3491 [person] => [time_id] => 9 [version_id] => 23188 [day] => 6 ) 
[41] => Array ( [id] => 3642 [person] => [time_id] => 9 [version_id] => 23212 [day] => 19 ) 
[42] => Array ( [id] => 3643 [person] => [time_id] => 9 [version_id] => 23212 [day] => 21 ) 
[43] => Array ( [id] => 3694 [person] => [time_id] => 15-- [version_id] => 23224 [day] => 10 ) 
[44] => Array ( [id] => 3695 [person] => [time_id] => 15-- [version_id] => 23224 [day] => 11 ) 
[45] => Array ( [id] => 3696 [person] => [time_id] => 15-- [version_id] => 23224 [day] => 12 ) 
[46] => Array ( [id] => 3697 [person] => [time_id] => 15-- [version_id] => 23224 [day] => 13 ) 
[47] => Array ( [id] => 3706 [person] => [time_id] => 9 [version_id] => 23225 [day] => 10 ) 
[48] => Array ( [id] => 3707 [person] => [time_id] => 9 [version_id] => 23225 [day] => 11 ) 
[49] => Array ( [id] => 3708 [person] => [time_id] => 9 [version_id] => 23225 [day] => 12 ) 
[50] => Array ( [id] => 3709 [person] => [time_id] => 9 [version_id] => 23225 [day] => 13 ) )

第一个数组是第二个数组的array_unique(version_id)

我需要创建一个新的二维数组,它将是这两个数组的总和。 第一个维度是第一个数组(version_is),第二个维度是天数(1-31)。 例如,如果第2,4,5天没有休息版本_id相等,则应该得到较低的version_id

例如

 Array (
    [0] => Array (
            [0] => Array  ( [version_id] => 23225 [day] => 1 [time_id] => 9 [person] => )
            [1] => Array  ( [version_id] => 23225 [day] => 2 [time_id] => 15 [person] => )
            [2] => Array  ( [version_id] => 23225 [day] => 15 [time_id] => 15 [person] => )
            [3] => Array  ( [version_id] => 23225 [day] => 30 [time_id] => 9+ [person] => ))
    [1] => Array (
            [0] => Array  ( [version_id] => 23224 [day] => 1 [time_id] => 9 [person] => )
            [1] => Array  ( [version_id] => 23224 [day] => 2 [time_id] => 17 [person] => )
            [2] => Array  ( [version_id] => 23224 [day] => 15 [time_id] => 15 [person] => )
            [3] => Array  ( [version_id] => 23224 [day] => 30 [time_id] => 5+ [person] => )))

我希望你理解我:)。

GOT IT! 你能检查一下这是最佳解决方案吗?

foreach ($Array as $key => $value) {
    foreach ($value as $skey => $svalue) {
        $version_id=$Array[$key]['version_id'];
        for ($day=1; $day<=31; $day++) {
            if ($Array[$key]['day']==$day) {
                $New[$version_id][$day]['day']=$Array[$key]['day'];
                $New[$version_id][$day]['person']=$Array[$key]['person'];
                $New[$version_id][$day]['time_id']=$Array[$key]['time_id'];
                $New[$version_id][$day]['who']=$Array[$key]['who'];     
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您需要创建一个二维数组。在c ++中你会做这样的事情:

int varArray[9][59];