php将一个二维数组按照某个字段值合并成一维数组,如果有重复则将重复的合并成二维数组

162 篇文章 0 订阅
114 篇文章 0 订阅

关注微信公众号:wwwcoder,现在已将博客搬到这里,内容精选过后才发布出来。谢谢大家支持


最近工作中碰到一个问题,用PHP将一个二维数组按照二维数组中的各个项中的某个特定字段值合并成一维数组,如果有重复则将重复的合并成二维数组,生成的二维数组的第一维的键是特定字段的值,二维的键可以是随机索引,也可以是其中的另一个字段的值。其实这个需求经常会在工作中碰到,只是碰到的时候一个有重复的就直接用之前的覆盖后面的或者用之后的覆盖之前的,这样很容易就可以处理了。很少碰到这种有一维数组又有二维数组的情况,先上代码:

$a = array(
    0 => array('target_id' => '22', 'target_type' => '1', 'phone_type' => '1', 'extension_number' => '15261', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => NULL,),
    1 => array('target_id' => '22', 'target_type' => '1', 'phone_type' => '2', 'extension_number' => '66002', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => '1',),
    14 => array('target_id' => '22', 'target_type' => '1','phone_type' => '4', 'extension_number' => '56431', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => NULL,),
    2 => array('target_id' => '24', 'target_type' => '1', 'phone_type' => '1', 'extension_number' => '54876', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => '1',),
    3 => array('target_id' => '25', 'target_type' => '1', 'phone_type' => '1', 'extension_number' => '07143', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => NULL,),
    13 => array('target_id' => '24', 'target_type' => '1', 'phone_type' => '0', 'extension_number' => '96949', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => NULL,),
    4 => array('target_id' => '26', 'target_type' => '1', 'phone_type' => '1', 'extension_number' => '94755', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => NULL,),
    5 => array('target_id' => '30', 'target_type' => '1', 'phone_type' => '1', 'extension_number' => '14171', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => NULL,),
    6 => array('target_id' => '36', 'target_type' => '1', 'phone_type' => '1', 'extension_number' => '54876', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => '1',),
    7 => array('target_id' => '42', 'target_type' => '1', 'phone_type' => '1', 'extension_number' => '31354', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => NULL,),
    8 => array('target_id' => '43', 'target_type' => '1', 'phone_type' => '1', 'extension_number' => '66810', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => NULL,),
    9 => array('target_id' => '45', 'target_type' => '1', 'phone_type' => '1', 'extension_number' => '34868', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => NULL,),
    10 => array('target_id' => '50', 'target_type' => '1', 'phone_type' => '1', 'extension_number' => '79523', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => NULL,),
    11 => array('target_id' => '149', 'target_type' => '1', 'phone_type' => '1', 'extension_number' => '59438', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => NULL,),
    12 => array('target_id' => '806', 'target_type' => '1', 'phone_type' => '1', 'extension_number' => '14780', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => NULL,),
);
$target = array();
$temp = array();
foreach ($a as $val) {
    if (!empty($target)) {
        if (in_array($val['target_id'], array_keys($target))) {
            if (!empty($temp[$val['target_id']])) {
                $target[$val['target_id']] = null;
                $target[$val['target_id']][$temp[$val['target_id']]['phone_type']] = $temp[$val['target_id']];
                $temp[$val['target_id']] = null;
            }
            $target[$val['target_id']][$val['phone_type']] = $val;
        } else {
            $temp[$val['target_id']] = $val;
            $target[$val['target_id']] = $val;
        }
    } else {
        $temp[$val['target_id']] = $val;
        $target[$val['target_id']] = $val;
    }
}
var_dump($target);exit;
这个返回的结果形如:

array
  22 => 
    array
      1 => 
        array
          'target_id' => string '22' (length=2)
          'target_type' => string '1' (length=1)
          'phone_type' => string '1' (length=1)
          'extension_number' => string '15261' (length=5)
          'phone_id' => string '1' (length=1)
          'phone_number' => string '4008285599' (length=10)
          'extension_special' => null
      2 => 
        array
          'target_id' => string '22' (length=2)
          'target_type' => string '1' (length=1)
          'phone_type' => string '2' (length=1)
          'extension_number' => string '66002' (length=5)
          'phone_id' => string '1' (length=1)
          'phone_number' => string '4008285599' (length=10)
          'extension_special' => string '1' (length=1)
      4 => 
        array
          'target_id' => string '22' (length=2)
          'target_type' => string '1' (length=1)
          'phone_type' => string '4' (length=1)
          'extension_number' => string '56431' (length=5)
          'phone_id' => string '1' (length=1)
          'phone_number' => string '4008285599' (length=10)
          'extension_special' => null
  24 => 
    array
      1 => 
        array
          'target_id' => string '24' (length=2)
          'target_type' => string '1' (length=1)
          'phone_type' => string '1' (length=1)
          'extension_number' => string '54876' (length=5)
          'phone_id' => string '1' (length=1)
          'phone_number' => string '4008285599' (length=10)
          'extension_special' => string '1' (length=1)
      0 => 
        array
          'target_id' => string '24' (length=2)
          'target_type' => string '1' (length=1)
          'phone_type' => string '0' (length=1)
          'extension_number' => string '96949' (length=5)
          'phone_id' => string '1' (length=1)
          'phone_number' => string '4008285599' (length=10)
          'extension_special' => null
  25 => 
    array
      'target_id' => string '25' (length=2)
      'target_type' => string '1' (length=1)
      'phone_type' => string '1' (length=1)
      'extension_number' => string '07143' (length=5)
      'phone_id' => string '1' (length=1)
      'phone_number' => string '4008285599' (length=10)
      'extension_special' => null
  26 => 
    array
      'target_id' => string '26' (length=2)
      'target_type' => string '1' (length=1)
      'phone_type' => string '1' (length=1)
      'extension_number' => string '94755' (length=5)
      'phone_id' => string '1' (length=1)
      'phone_number' => string '4008285599' (length=10)
      'extension_special' => null
  30 => 
    array
      'target_id' => string '30' (length=2)
      'target_type' => string '1' (length=1)
      'phone_type' => string '1' (length=1)
      'extension_number' => string '14171' (length=5)
      'phone_id' => string '1' (length=1)
      'phone_number' => string '4008285599' (length=10)
      'extension_special' => null
  36 => 
    array
      'target_id' => string '36' (length=2)
      'target_type' => string '1' (length=1)
      'phone_type' => string '1' (length=1)
      'extension_number' => string '54876' (length=5)
      'phone_id' => string '1' (length=1)
      'phone_number' => string '4008285599' (length=10)
      'extension_special' => string '1' (length=1)
  42 => 
    array
      'target_id' => string '42' (length=2)
      'target_type' => string '1' (length=1)
      'phone_type' => string '1' (length=1)
      'extension_number' => string '31354' (length=5)
      'phone_id' => string '1' (length=1)
      'phone_number' => string '4008285599' (length=10)
      'extension_special' => null
  43 => 
    array
      'target_id' => string '43' (length=2)
      'target_type' => string '1' (length=1)
      'phone_type' => string '1' (length=1)
      'extension_number' => string '66810' (length=5)
      'phone_id' => string '1' (length=1)
      'phone_number' => string '4008285599' (length=10)
      'extension_special' => null
  45 => 
    array
      'target_id' => string '45' (length=2)
      'target_type' => string '1' (length=1)
      'phone_type' => string '1' (length=1)
      'extension_number' => string '34868' (length=5)
      'phone_id' => string '1' (length=1)
      'phone_number' => string '4008285599' (length=10)
      'extension_special' => null
  50 => 
    array
      'target_id' => string '50' (length=2)
      'target_type' => string '1' (length=1)
      'phone_type' => string '1' (length=1)
      'extension_number' => string '79523' (length=5)
      'phone_id' => string '1' (length=1)
      'phone_number' => string '4008285599' (length=10)
      'extension_special' => null
  149 => 
    array
      'target_id' => string '149' (length=3)
      'target_type' => string '1' (length=1)
      'phone_type' => string '1' (length=1)
      'extension_number' => string '59438' (length=5)
      'phone_id' => string '1' (length=1)
      'phone_number' => string '4008285599' (length=10)
      'extension_special' => null
  806 => 
    array
      'target_id' => string '806' (length=3)
      'target_type' => string '1' (length=1)
      'phone_type' => string '1' (length=1)
      'extension_number' => string '14780' (length=5)
      'phone_id' => string '1' (length=1)
      'phone_number' => string '4008285599' (length=10)
      'extension_special' => null

如果不想要生成的二维数组的key值,或者说再精简些,可以这样:

$a = array(
    0 => array('target_id' => '22', 'target_type' => '1', 'phone_type' => '1', 'extension_number' => '15261', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => NULL,),
    1 => array('target_id' => '22', 'target_type' => '1', 'phone_type' => '2', 'extension_number' => '66002', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => '1',),
    14 => array('target_id' => '22', 'target_type' => '1','phone_type' => '4', 'extension_number' => '56431', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => NULL,),
    2 => array('target_id' => '24', 'target_type' => '1', 'phone_type' => '1', 'extension_number' => '54876', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => '1',),
    3 => array('target_id' => '25', 'target_type' => '1', 'phone_type' => '1', 'extension_number' => '07143', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => NULL,),
    13 => array('target_id' => '24', 'target_type' => '1', 'phone_type' => '0', 'extension_number' => '96949', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => NULL,),
    4 => array('target_id' => '26', 'target_type' => '1', 'phone_type' => '1', 'extension_number' => '94755', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => NULL,),
    5 => array('target_id' => '30', 'target_type' => '1', 'phone_type' => '1', 'extension_number' => '14171', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => NULL,),
    6 => array('target_id' => '36', 'target_type' => '1', 'phone_type' => '1', 'extension_number' => '54876', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => '1',),
    7 => array('target_id' => '42', 'target_type' => '1', 'phone_type' => '1', 'extension_number' => '31354', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => NULL,),
    8 => array('target_id' => '43', 'target_type' => '1', 'phone_type' => '1', 'extension_number' => '66810', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => NULL,),
    9 => array('target_id' => '45', 'target_type' => '1', 'phone_type' => '1', 'extension_number' => '34868', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => NULL,),
    10 => array('target_id' => '50', 'target_type' => '1', 'phone_type' => '1', 'extension_number' => '79523', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => NULL,),
    11 => array('target_id' => '149', 'target_type' => '1', 'phone_type' => '1', 'extension_number' => '59438', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => NULL,),
    12 => array('target_id' => '806', 'target_type' => '1', 'phone_type' => '1', 'extension_number' => '14780', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => NULL,),
);
$target = array();
$temp = array();
foreach ($a as $val) {
    if (!empty($target)) {
        if (in_array($val['target_id'], array_keys($target))) {
            if (!empty($temp[$val['target_id']])) {
                $target[$val['target_id']] = null;
                $target[$val['target_id']][] = $temp[$val['target_id']];
                $temp[$val['target_id']] = null;
            }
            $target[$val['target_id']][] = $val;
        } else {
            $temp[$val['target_id']] = $val;
            $target[$val['target_id']] = $val;
        }
    } else {
        $temp[$val['target_id']] = $val;
        $target[$val['target_id']] = $val;
    }
}
var_dump($target);exit;
生成结果如:

array
  22 => 
    array
      0 => 
        array
          'target_id' => string '22' (length=2)
          'target_type' => string '1' (length=1)
          'phone_type' => string '1' (length=1)
          'extension_number' => string '15261' (length=5)
          'phone_id' => string '1' (length=1)
          'phone_number' => string '4008285599' (length=10)
          'extension_special' => null
      1 => 
        array
          'target_id' => string '22' (length=2)
          'target_type' => string '1' (length=1)
          'phone_type' => string '2' (length=1)
          'extension_number' => string '66002' (length=5)
          'phone_id' => string '1' (length=1)
          'phone_number' => string '4008285599' (length=10)
          'extension_special' => string '1' (length=1)
      2 => 
        array
          'target_id' => string '22' (length=2)
          'target_type' => string '1' (length=1)
          'phone_type' => string '4' (length=1)
          'extension_number' => string '56431' (length=5)
          'phone_id' => string '1' (length=1)
          'phone_number' => string '4008285599' (length=10)
          'extension_special' => null
  24 => 
    array
      0 => 
        array
          'target_id' => string '24' (length=2)
          'target_type' => string '1' (length=1)
          'phone_type' => string '1' (length=1)
          'extension_number' => string '54876' (length=5)
          'phone_id' => string '1' (length=1)
          'phone_number' => string '4008285599' (length=10)
          'extension_special' => string '1' (length=1)
      1 => 
        array
          'target_id' => string '24' (length=2)
          'target_type' => string '1' (length=1)
          'phone_type' => string '0' (length=1)
          'extension_number' => string '96949' (length=5)
          'phone_id' => string '1' (length=1)
          'phone_number' => string '4008285599' (length=10)
          'extension_special' => null
  25 => 
    array
      'target_id' => string '25' (length=2)
      'target_type' => string '1' (length=1)
      'phone_type' => string '1' (length=1)
      'extension_number' => string '07143' (length=5)
      'phone_id' => string '1' (length=1)
      'phone_number' => string '4008285599' (length=10)
      'extension_special' => null
  26 => 
    array
      'target_id' => string '26' (length=2)
      'target_type' => string '1' (length=1)
      'phone_type' => string '1' (length=1)
      'extension_number' => string '94755' (length=5)
      'phone_id' => string '1' (length=1)
      'phone_number' => string '4008285599' (length=10)
      'extension_special' => null
  30 => 
    array
      'target_id' => string '30' (length=2)
      'target_type' => string '1' (length=1)
      'phone_type' => string '1' (length=1)
      'extension_number' => string '14171' (length=5)
      'phone_id' => string '1' (length=1)
      'phone_number' => string '4008285599' (length=10)
      'extension_special' => null
  36 => 
    array
      'target_id' => string '36' (length=2)
      'target_type' => string '1' (length=1)
      'phone_type' => string '1' (length=1)
      'extension_number' => string '54876' (length=5)
      'phone_id' => string '1' (length=1)
      'phone_number' => string '4008285599' (length=10)
      'extension_special' => string '1' (length=1)
  42 => 
    array
      'target_id' => string '42' (length=2)
      'target_type' => string '1' (length=1)
      'phone_type' => string '1' (length=1)
      'extension_number' => string '31354' (length=5)
      'phone_id' => string '1' (length=1)
      'phone_number' => string '4008285599' (length=10)
      'extension_special' => null
  43 => 
    array
      'target_id' => string '43' (length=2)
      'target_type' => string '1' (length=1)
      'phone_type' => string '1' (length=1)
      'extension_number' => string '66810' (length=5)
      'phone_id' => string '1' (length=1)
      'phone_number' => string '4008285599' (length=10)
      'extension_special' => null
  45 => 
    array
      'target_id' => string '45' (length=2)
      'target_type' => string '1' (length=1)
      'phone_type' => string '1' (length=1)
      'extension_number' => string '34868' (length=5)
      'phone_id' => string '1' (length=1)
      'phone_number' => string '4008285599' (length=10)
      'extension_special' => null
  50 => 
    array
      'target_id' => string '50' (length=2)
      'target_type' => string '1' (length=1)
      'phone_type' => string '1' (length=1)
      'extension_number' => string '79523' (length=5)
      'phone_id' => string '1' (length=1)
      'phone_number' => string '4008285599' (length=10)
      'extension_special' => null
  149 => 
    array
      'target_id' => string '149' (length=3)
      'target_type' => string '1' (length=1)
      'phone_type' => string '1' (length=1)
      'extension_number' => string '59438' (length=5)
      'phone_id' => string '1' (length=1)
      'phone_number' => string '4008285599' (length=10)
      'extension_special' => null
  806 => 
    array
      'target_id' => string '806' (length=3)
      'target_type' => string '1' (length=1)
      'phone_type' => string '1' (length=1)
      'extension_number' => string '14780' (length=5)
      'phone_id' => string '1' (length=1)
      'phone_number' => string '4008285599' (length=10)
      'extension_special' => null
我这样写有些复杂,但是这种需求在网上很难搜到代码,所以自己贴一份,给大家审查下,看看还有什么更简单的方法实现,毕竟我的算法学的还是挺差的。欢迎指点讨论谢谢。


















  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值