根据键合并相同的数组索引并构建一个新数组

时间:2014-09-23 06:04:04

标签: php arrays php-5.5

我正在尝试合并诸如
的数组索引 011,021,031,012,022,032,013,023,033,014,024,034。

$temp = array();
foreach($samplearray as $key => $val){
    foreach($val as $key1 => $val1){
       //what should logic here     
    }
}

示例数组

$samplearray = array ( "AA"  => array ( 0  => array ( "created by" => "011",
                                                "date" => "12-03-14",
                                                "time" => "12.00 pm"
                                                ),
                                1  => array ( "created by" => "012",
                                                "date" => "12-03-14",
                                                "time" => "12.00 pm"
                                            ),
                                2  => array ( "created by" => "013",
                                                "date" => "12-03-14",
                                                "time" => "12.00 pm"
                                            ),
                                3  => array ( "created by" => "014",
                                                "date" => "12-03-14",
                                                "time" => "12.00 pm"
                                            )               
                                ),
                "BB"  => array ( 0  => array ( "created by" => "021",
                                                "date" => "12-03-14",
                                                "time" => "12.00 pm"
                                            ),
                                1  => array ( "created by" => "022",
                                                "date" => "12-03-14",
                                                "time" => "12.00 pm"
                                            ),
                                2  => array ( "created by" => "023",
                                                "date" => "12-03-14",
                                                "time" => "12.00 pm"
                                            ),
                                3  => array ( "created by" => "024",
                                                "date" => "12-03-14",
                                                "time" => "12.00 pm"
                                            )               
                                ),
                "CC"  => array ( 0  => array ( "created by" => "031",
                                                "date" => "12-03-14",
                                                "time" => "12.00 pm"
                                            ),
                                1  => array ( "created by" => "032",
                                                "date" => "12-03-14",
                                                "time" => "12.00 pm"
                                            ),
                                2  => array ( "created by" => "033",
                                                "date" => "12-03-14",
                                                "time" => "12.00 pm"
                                            ),
                                3  => array ( "created by" => "034",
                                                "date" => "12-03-14",
                                                "time" => "12.00 pm"
                                            )               
                                )
                );

必需输出

$newArray = array ( 0  => array ( "created by" => "011",
                                    "date" => "12-03-14",
                                    "time" => "12.00 pm"
                                ),
                    1  => array ( "created by" => "021",
                                "date" => "12-03-14",
                                "time" => "12.00 pm"
                                ),
                    2  => array ( "created by" => "031",
                                "date" => "12-03-14",
                                "time" => "12.00 pm"
                                ),
                    3  => array ( "created by" => "012",
                                "date" => "12-03-14",
                                "time" => "12.00 pm"
                                ),              
                    4  => array ( "created by" => "022",
                                    "date" => "12-03-14",
                                    "time" => "12.00 pm"
                                ),
                    5  => array ( "created by" => "032",
                                "date" => "12-03-14",
                                "time" => "12.00 pm"
                                ),
                    6  => array ( "created by" => "013",
                                "date" => "12-03-14",
                                "time" => "12.00 pm"
                                ),
                    7  => array ( "created by" => "023",
                                "date" => "12-03-14",
                                "time" => "12.00 pm"
                                ),
                    8  => array ( "created by" => "033",
                                    "date" => "12-03-14",
                                    "time" => "12.00 pm"
                                ),
                    9  => array ( "created by" => "014",
                                "date" => "12-03-14",
                                "time" => "12.00 pm"
                                ),
                    10  => array ( "created by" => "024",
                                "date" => "12-03-14",
                                "time" => "12.00 pm"
                                ),
                    11  => array ( "created by" => "034",
                                "date" => "12-03-14",
                                "time" => "12.00 pm"
                                ),                          
                );

2 个答案:

答案 0 :(得分:3)

实际上它非常简单。提示已经在标题中。 array_merge()

$newArray = array();
foreach ($samplearray as $key => $batch) {
    $newArray = array_merge($newArray, $batch);
}

要遵循这种特殊格式,您可以每次移动它们,直到$samplearray用完为止。

示例:

$newArray = array();
while(true) {
    foreach ($samplearray as $key => &$batch) {
        $newArray[] = array_shift($batch); // shift each batch
    }

    if(empty($batch)) { // if it runs out, stop!
        break;
    }
}

答案 1 :(得分:0)

试试这段代码

$temp = array();
foreach($samplearray as $key => $val){
   foreach($val as $key1 => $val1){
       $temp[] = $val1;
   }