版权声明:本文为博主原创文章,未经博主允许不得转载。

最近工作中碰到一个问题,用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将一个二维数组按照某个字段值合并成一维数组,如果有重复则将重复的合并成二维数组的更多相关文章

  1. PHP二位数组按照数组的某个字段值排序

    不多废话 直接代码 /** * @name 排序 按照数组的某个字段值排序 * @param $array 排序数组 $field 排序字段 $direction 排序顺序 * @author wan ...

  2. PHP 二维数组根据某个字段排序

    二维数组根据某个字段排序有两种办法,一种是通过sort自己写代码,一种是直接用array_multisort排序函数 一. 手写arraysort PHP的一维数组排序函数: sort  对数组的值按 ...

  3. js将一个具有相同键值对的一维数组转换成二维数组

    这两天,一个前端朋友在面试的笔试过程中遇到了一道类似于"用js实现将一个具有相同code值的一维数组转换成相同code值在一起的二维数组"的题目.他面试过后,把这个问题抛给了我,问 ...

  4. php二维数组根据某个字段去重

    php的二维数组根据某个字段去重,在这默认为二维数组的结构是一样的,现在根据二维数组里的id字段去重,把id相同的重复的元素去掉 /** * 二维数组根据某个字段去重 * @param array $ ...

  5. PHP二维数组按某个字段排序

    //准备 二维数组 //按一个字段排序 foreach($rank as $key=>$val){ $dos[$key] = $val['timelength']; } array_multis ...

  6. PHP 二维数组根据某个字段按指定排序方式排序

    /** * 二维数组根据某个字段按指定排序方式排序 * @param $arr array 二维数组 * @param $field string 指定字段 * @param int $sort_or ...

  7. extract_by_one 根据二维数组中某字段来提取数组信息,查看有无重复信息

    public function tt(){ $param = array( array ( 'hykno' => '2222222-CB', 'tcdk_fid' => '458B6D70 ...

  8. 一个有用的排序函数,array_multisort(),下面的一个用法是根据二维数组里的一个字段值的大小,对该二维数组进行重新排序

    从二维数组$cashes中取出一列 'store_id'(二维数组中的每个一维数组都有的字段),按照这个的大小排序,对二维数组$caches里面的一维数组进行重新排序 实际应用如下 想让相同部门的排在 ...

  9. PHP中根据二维数组中某个字段实现排序

    想要实现二维数组中根据某个字段排序,一般可以通过数组循环对比的方式实现.这里介绍一种更简单的方法,直接通过PHP函数实现.array_multisort() :可以用来一次对多个数组进行排序,或者根据 ...

随机推荐

  1. 如何落地全球最大 Kubernetes 生产集群

        鲍永成   京东基础架构部技术总监,   DevOps 标准核心编写专家   前言   JDOS 就是京东数据中心操作系统,随着数据中心规模不断的扩大,我们需要对数据中心做综合的考虑.所以一开 ...

  2. Python鸡汤

    标准库 很正确 外部库 有一些风险,可能有bug,可能文档不全,可能长时间未更新. ipython 1 pip 这应该是安装Python后第一个需要的命令 pip install -i -i, --i ...

  3. 多媒体开发之--- h264 图像、帧、片、NALU

    图像.帧.片.NALU 是学习 H.264的人常常感到困惑的一些概念,我在这里对自己的理解做一些阐述,欢迎大家讨论: H.264 是一次概念的革新,它打破常规,完全没有 I 帧.P帧.B 帧的概念,也 ...

  4. YUM安装(卸载)KDE和GNOME

    YUM安装(卸载)KDE和GNOME显示系统已经安装的组件,和可以安装的组件:#yum grouplist 如果系统安装之初采用最小化安装,没有安装xwindow,那么先安装:#yum groupin ...

  5. PHP-Manual的学习----【语言参考】----【类型】-----【Boolean类型】

    2017年7月20日15:41:26Boolean 布尔类型 1.这是最简单的类型.boolean 表达了真值,可以为 TRUE 或 FALSE. 其实就是真假的问题.2.语法 要指定一个布尔值,使用 ...

  6. bash批量去前缀

    #!/bin/sh for aFile in *; do oldfile=`basename "$aFile"` newfile=${oldfile::} echo ${oldfi ...

  7. 1.BeanFactory解析

    package org.springframework.beans.factory; import org.springframework.beans.BeansException; import o ...

  8. swift打印方法名文件路径

    1.打印方法名 print(#function) 1.打印文件路径 print(#file)

  9. images have the “stationarity” property, which implies that features that are useful in one region are also likely to be useful for other regions.

    Convolutional networks may include local or global pooling layers[clarification needed], which combi ...

  10. Linux安装Nginx使用反向代理

    nginx的反向代理功能(自带了反向代理的功能,天生的二道贩子)1.实验环境准备准备2个服务器,都安装好nginx软件nginx1 192.168.13.79 作为web服务器 (理解为火车票售票点) ...