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

  我这样写有些复杂,但是这种需求在网上很难搜到代码,所以自己贴一份,给大家审查下,看看还有什么更简单的方法实现,毕竟我的算法学的还是挺差的。欢迎指点讨论谢谢。

时间: 2024-10-12 20:40:51

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

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

原文:PHP 二维数组根据某个字段排序 要求:从两个不同的表中获取各自的4条数据,然后整合(array_merge)成一个数组,再根据数据的创建时间降序排序取前4条. 遇到这个要求的时候就不是 ORDER BY 能解决的问题了.因此翻看 PHP 手册查找到了如下方法,做此笔记. <?php /** * 二维数组根据某个字段排序 * 功能:按照用户的年龄倒序排序 * @author ruxing.li */ header('Content-Type:text/html;Charset=utf-8'

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

想要实现二维数组中根据某个字段排序,一般可以通过数组循环对比的方式实现.这里介绍一种更简单的方法,直接通过PHP函数实现.array_multisort() :可以用来一次对多个数组进行排序,或者根据某一维或多维对多维数组进行排序.详细介绍可参考PHP手册:https://www.php.net/manual/zh/function.array-multisort.php 实例: 1.单个字段排序: $data = [ ['id' => 1, 'name' => '张三', 'sort' =&

php 二维数组 根据某个字段排序

/*** 数组排序 根据某个字段排序 * @author ganyuanjiang <[email protected]>* @createtime 2017-07-30 09:31:48* @param $arr array() 要排序的数组* @param $sort string 排序类型 asc顺序 desc逆序* @param $field string 要排序的字段 * @return array() 排序过的数组*/if(!function_exists('arr_sort'))

在主方法中定义一个大小为10*10的二维字符型数组,数组名为y,正反对角线上存的是‘*’,其余 位置存的是‘#’;输出这个数组中的所有元素。

//在主方法中定义一个大小为10*10的二维字符型数组,数组名为y,正反对角线上存的是‘*’,其余 位置存的是‘#’:输出这个数组中的所有元素. char [][]y=new char [10][10]; for(int i=0;i<10;i++) { for(int j=0;j<10;j++) { if(i==j||i+j==9) { y[i][j]='*'; } else { y[i][j]='#'; } } } for(int i =0;i<10;i++) { for(int k

PHP二维数组(或任意维数组)转换成一维数组的方法汇总

目录 1 array_reduce函数法 2 array_walk_recursive函数法 3 array_map函数法 假设有下面一个二维数组: $user = array( '0' => array('id' => 100, 'username' => 'a1'), '1' => array('id' => 101, 'username' => 'a2'), '2' => array('id' => 102, 'username' => 'a3'

PHP将二位数组按照第二维的某个元素的值进行排序

例如: 1 //原始数组是这样的,希望能够按照第二维中的run_date升序或者降序进行排序: 2 $arr=array( 3 0=>array( 4 'run_date'=>'2017-11-21', 5 'count'=>'5' 6 ), 7 1=>array( 8 'run_date'=>'2017-11-20', 9 'count'=>'10' 10 ), 11 2=>array( 12 'run_date'=>'2017-11-22', 13 'c

抓取二维数组某值出来,到一维数组

/*** * '抓取二维数组某值出来,到一维数组' * @param $arr * @param $item * @return array */ function get_arr_item_val($arr,$item){ if (!$arr){ return []; } $res = []; foreach ($arr as $v){ $res[] = $v[$item]; } return $res; } 原文地址:https://www.cnblogs.com/pansidong/p/1

PHP二维数组转换成一维数组,一个变量多个二维数组转换成一维数组,PHP二维数组(或任意维数组)转换成一维数组的方法汇总,array_reduce(); array_walk_recursive(); array_map();

方法汇总: 1. array_reduce函数法 //用array_reduce()函数是较为快捷的方法: $result = array_reduce($user, function ($result, $value) { return array_merge($result, array_values($value)); }, array()) 2. array_walk_recursive函数法 //用array_walk_recursive()函数就非常灵活,可以把任意维度的数组转换成一

将一个一维数组的元素赋值给另外一个一维数组中

//数组拷贝 int a[5] = {37,25,18,-2,78}; int b[5] = {0}; for (int i = 0; i < 5; i++) { b[i] = a[i]; printf("%d ",b[i]); } 将一个一维数组的元素赋值给另外一个一维数组中