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

最近工作中碰到一个问题,用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. IntelliJ idea——》创建tag、删除tag

    https://blog.csdn.net/weixin_43453386/article/details/83857038

  2. spring boot集成activemq

    spring boot集成activemq 转自:https://blog.csdn.net/maiyikai/article/details/77199300

  3. QT应用程序 安装路径中文异常问题

    [1]QT 安装中文路径启动异常问题 最近在搞一个很简单的QT应用程序,开发环境VS2017 + QT5.9,线上异常报错:安装中文路径下启动崩溃~~~~ 最后,本地调试Debug版本,发现安装中文路 ...

  4. mysql 存储过程初探

    使用存储过程好处在于: 1.隐藏敏感的算法,避免被正常的开发人员看到,把业务逻辑隐藏在数据库中,而非程序代码里 2.简化应用代码程序,放到数据库里肯定就对程序代码简化有好处了 3.不同的开发语言都可以 ...

  5. json格式转数组注意事项

    今天遇到一个特别奇葩的问题,json格式明明是正确的,转数组时却就是出不来,后来才发现是文件的编码问题,文件的编码一定要是utf-8无BOM格式,这点一定要切记!切记! 如果想确认json是否正确:h ...

  6. ResfulApi规范

    序号 方法 描述 1 GET 请求指定的页面信息,并返回实体主体. 2 HEAD 类似于get请求,只不过返回的响应中没有具体的内容,用于获取报头 3 POST 向指定资源提交数据进行处理请求(例如提 ...

  7. Arrays数组工具类中存在的坑!

    以下是一个坑: 看到使用asList时候,可以返回一个集合,当然集合拥有CRUD的特性: 代码中使用 了add和remove时候均报错:Exception in thread "main&q ...

  8. Qt状态机框架(状态机就开始异步的运行了,也就是说,它成为了我们应用程序事件循环的一部分了)

    状态机框架 Qt中的状态机框架为我们提供了很多的API和类,使我们能更容易的在自己的应用程序中集成状态动画.这个框架是和Qt的元对象系统机密结合在一起的.比如,各个状态之间的转换是通过信号触发的,状态 ...

  9. CMDB的四种模式

    为什么要有CMDB? CMDB --Configuration Management Database 配置管理数据库. 1.为了实现资产的自动采集,资产的自动更新, 为了搭建公司自动化平台的基础则需 ...

  10. spark0.9.0安装

    利用周末的时间安装学习了下最近很火的Spark0.9.0(江湖传言,要革hadoop命,O(∩_∩)O),并体验了该框架下的机器学习包MLlib(spark解决的一个重点就是高效的运行迭代算法),下面 ...