drc15469 2013-07-23 04:32
浏览 33
已采纳

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>
  • 写回答

1条回答 默认 最新

  • dongwuying0221 2013-07-23 06:57
    关注
    $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));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?