php - 有可能合并两个具有不同元素编号的2d数组吗? [重复]

This question already has an answer here:

I have 3 set of 2d array that have different element number?One is the array that need to be as key(date and time) to compare with other two array.The other two array must in sequence.For row that empty, i want set as -9999.

First array :

Array (   
       [0] => Array
          (
            [0] => 01/7/2013
            [1] => 12.00 a.m

          )

       [1] => Array
          (
            [0] => 01/7/2013
            [1] => 12.01 a.m

          )

       [2] => Array
          (
            [0] => 01/7/2013
            [1] => 12.02 a.m

          )

      )

For second array:

Array(   

   [0] => Array
       (
          [0] => 01/7/2013
          [1] => 12.01 a.m
          [2] => 9.02
       )
 )

For Third array:

 Array(   
    [0] => Array
        (
            [0] => 01/7/2013
            [1] => 12.00 a.m
            [2] => 1.23
            [3] => 6.1
        )

    [1] => Array
        (
            [0] => 01/7/2013
            [1] => 12.02 a.m
            [2] => 1.75
            [3] => 1.75
        )

  )

and i need the output as:

Array(   
    [0] => Array
        (
            [0] => 01/7/2013
            [1] => 12.00 a.m
            [2] => -9999
            [3] => 1.23
            [4] => 6.1
        )

    [1] => Array
        (
            [0] => 01/7/2013
            [1] => 12.01 a.m
            [2] => 9.02
            [3] => -9999
            [4] => -9999
        )
    [2] => Array
        (
            [0] => 01/7/2013
            [1] => 12.02 a.m
            [2] => -9999
            [3] => 1.75
            [4] => 1.75
        )
 )
</div>
$arr = array_merge($arr1, $arr2, $arr3);
$out = array();
$cnt = 0;
foreach ($arr as $key => $value){
    $date = explode('/', $value[0]);//Change date format
    $dex = strtotime($date[1].'/'.$date[0].'/'.$date[2].' '.$value[1]);
    $head = (array_key_exists($dex, $out)) ? $out[$dex] : array_slice($value, 0, 2);
    $out[$dex] = array_merge($head, array_slice($value, 2));
    $cnt = ($cnt > count($out[$dex])) ? $cnt : count($out[$dex]);
}
$out = array_map(function ($e) use ($cnt){
    return array_pad($e, $cnt, '-9999');
}, array_values($out));
print_r($out);

That works for PHP 5.3 and later. For older versions of PHP, you can replace the array_map function with the following:

foreach ($out as $key => $value){
    $res[] = array_pad($value, $cnt, -9999);
}
print_r($res);

NEW VERSION (for PHP 5.3 or later):

$template = array();
array_walk($arr1, function (&$e, $k) use (&$template){
    $date = explode('/', $e[0]);
    $dex = strtotime($date[1].'/'.$date[0].'/'.$date[2].' '.$e[1]);
    $template[$dex][0] = '-9999';
});
$arr1 = array_combine(array_keys($template), $arr1);
$out = array();
$data = array($arr2, $arr3);
foreach ($data as $key => $value){
    foreach ($value as $key2 => $value2){
        $date = explode('/', $value2[0]);//Change date format
        $dex = strtotime($date[1].'/'.$date[0].'/'.$date[2].' '.$value2[1]);
        $out[$key][$dex] = array_slice($value2, 2);
    }
    $out[$key] += array_diff_key($template, $out[$key]);
    foreach ($out[$key] as $key2 => $value2){
        $arr1[$key2] = array_merge($arr1[$key2], $value2);
    }
}
print_r(array_values($arr1));