2017-10-12 13:34:02 2646次浏览 2条回答 0 悬赏 45 金钱
Array(
    [0] => Array
        (
            [created_at] => 2016-08
            [a] => 31900.00
        )
    [1] => Array
        (
            [created_at] => 2016-09
            [a] => 45400.00
        )
    [2] => Array
        (
            [created_at] => 2016-10
            [a] => 69489.00
        )
)

Array
(
    [0] => Array
        (
            [created_at] => 2016-08
            [b] => 21900.00
        )
    [1] => Array
        (
            [created_at] => 2016-09
            [b] => 25400.00
        )
)

怎么合并成下面数组啊

Array
(
    [0] => Array
        (
            [created_at] => 2016-08
            [a] => 31900.00
            [b] => 21900.00
        )
    [1] => Array
        (
            [created_at] => 2016-09
            [a] => 45400.00
            [b] => 25400.00
        )
    [2] => Array
        (
            [created_at] => 2016-10
            [a] => 69489.00
            [b] => 
        )
)

最佳答案

  • mercis 发布于 2017-10-12 14:02 举报
    $arr1 = Array(
        0 => Array
        (
            'created_at' => '2016 - 08',
            'a' => 31900.00
        ),
        1 => Array
        (
            'created_at' => '2016 - 09',
            'a' => 45400.00
        ),
        2 => Array
        (
            'created_at' => '2016 - 10',
            'a' => 69489.00
        ));
    
    $arr2 = Array(
        0 => Array
        (
            'created_at' => '2016 - 08',
            'b' => 21900.00
        ),
        1 => Array
        (
            'created_at' => '2016 - 09',
            'b' => 25400.00
        ));
    $arr3 = [];
    foreach ($arr1 as $key => $val) {
        $arr3[$key]['created_at'] = $val['created_at'];
        $arr3[$key]['a'] = $val['a'];
        foreach ($arr2 as $v) {
            if ($arr3[$key]['created_at'] == $v['created_at']) {
                $arr3[$key]['b'] = $v['b'];
            } else {
                $arr3[$key]['b'] = '';
            }
        }
    }
    var_dump($arr3);
    

    结果 :

    array (size=3)
      0 => 
        array (size=3)
          'created_at' => string '2016 - 08' (length=9)
          'a' => float 31900
          'b' => string '' (length=0)
      1 => 
        array (size=3)
          'created_at' => string '2016 - 09' (length=9)
          'a' => float 45400
          'b' => float 25400
      2 => 
        array (size=3)
          'created_at' => string '2016 - 10' (length=9)
          'a' => float 69489
          'b' => string '' (length=0)
          
    
    4 条回复
    回复于 2017-10-12 14:51 回复
    array (size=3)
      'created_at' => string '2016 - 08' (length=9)
      'a' => float 31900
      'b' => string '' (length=0)
    

    ‘b' 不应该空的!应该修改一下,判断键是否已经设置,如下(我的变量与此不同):内层循环修改一下

    if (!array_key_exists('b', $d[$k1]))
       $d[$k1]['b'] = $d[$k1]['created_at'] == $v2['created_at'] ? $v2['b'] : '';
    
    回复于 2017-10-13 08:48 回复

    这段代码只是当时的应用场景(已知两个数组),深究的话还是有很多问题,不过还是感谢你的意见

    回复于 2017-10-19 18:35 回复

    //一般这种情况 代码一般要写通用一点 假设不知道两个数组的内容 只是合并而已

        $_tmp = $arr3 = [];
        count($arr1) > count($arr2) ? $_tmp = $arr1 : $_tmp = $arr2;
        foreach($_tmp as $key => $val){
           if(isset($arr1[$key]) && isset($arr2[$key])){
               $t1 = array_diff($arr1[$key],$arr2[$key]);
               $t2 = array_diff($arr2[$key] ,$arr1[$key]);
               $t3 = array_intersect($arr2[$key],$arr1[$key]);
               $arr3[$key] =array_merge($t3,$t1,$t2);
           }else{
               $arr3[$key] = $val;
           }
        }
        //假设你要二维数组下的键名都一样 如果不存在值就以空代替 可以再把结果foreach循环 更改 其实这样没有什么意义的 你view遍历的时候大致可以isset下就可以了 没有必要再这样去组合
        
        echo"<pre>";print_r($arr3);exit;
    
    回复于 2017-10-20 08:51 回复

    这个有意思了,还有没有别的解法

  • 回答于 2017-10-12 14:39 举报

    以下代码可以满足你的要求,尽管这个要求有点奇怪

    $a = [
        ['created_at' => '2016-8', 'a' => 31900.00],
        ['created_at' => '2016-6', 'a' => 11900.00],
        ['created_at' => '2016-9', 'a' => 45400.00],
        ['created_at' => '2016-10', 'a' => 69489.00],
    ];
    
    $b = [
        ['created_at' => '2016-8', 'b' => 21900.00],
        ['created_at' => '2016-9', 'b' => 25400.00],
    ];
    $aa = [];
    $bb = [];
    foreach ($a as $item) {
        $aa[$item['created_at']] = $item;
    }
    foreach ($b as $item) {
        $bb[$item['created_at']] = $item;
    }
    $cc = array_merge_recursive($aa, $bb);
    $c = array_map($f = function ($v) {
        $v['created_at'] = $v['created_at'][0];
        return $v;
    }, $cc);
    var_dump(array_values($c));
    

    从常规索引数组到关联数组,没有想到好办法,只能循环了。
    很难判断算法在大数据量时的优缺点

您需要登录后才可以回答。登录 | 立即注册
平头哥
总监

平头哥 山沟沟

注册时间:2016-04-25
最后登录:2020-07-23
在线时长:59小时17分
  • 粉丝9
  • 金钱2000
  • 威望30
  • 积分2890

热门问题