php将一个二维数组按照某个字段值合并成一维数组,如果有重复则将重复的合并成二维数组
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php将一个二维数组按照某个字段值合并成一维数组,如果有重复则将重复的合并成二维数组相关的知识,希望对你有一定的参考价值。
版权声明:本文为博主原创文章,未经博主允许不得转载。
最近工作中碰到一个问题,用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
我这样写有些复杂,但是这种需求在网上很难搜到代码,所以自己贴一份,给大家审查下,看看还有什么更简单的方法实现,毕竟我的算法学的还是挺差的。欢迎指点讨论谢谢。
以上是关于php将一个二维数组按照某个字段值合并成一维数组,如果有重复则将重复的合并成二维数组的主要内容,如果未能解决你的问题,请参考以下文章