Rumah  >  Artikel  >  pembangunan bahagian belakang  >  php数组实现根据某个键值将相同键值合并生成新二维数组的方法详解

php数组实现根据某个键值将相同键值合并生成新二维数组的方法详解

墨辰丷
墨辰丷asal
2018-05-23 14:56:282182semak imbas

这篇文章主要介绍了php数组实现根据某个键值将相同键值合并生成新二维数组的方法,涉及php数组的遍历、赋值相关运算技巧,需要的朋友可以参考下

本文实例讲述了php数组实现根据某个键值将相同键值合并生成新二维数组的方法。分享给大家供大家参考,具体如下:

源数据:

$infos = array(
  array(
    'a' => 36,
    'b' => 'xa',
    'c' => '2015-08-28 00:00:00',
    'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'
  ),
  array(
    'a' => 3,
    'b' => 'vd',
    'c' => '2015-08-20 00:00:00',
    'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'
  ),
  array(
    'a' => 6,
    'b' => 'wwe',
    'c' => '2015-08-28 00:00:00',
    'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'
  ),
  array(
    'a' => 36,
    'b' => 'se',
    'c' => '2015-08-28 00:00:00',
    'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'
  ),
  array(
    'a' => 6,
    'b' => 'aw',
    'c' => '2015-08-28 00:00:00',
    'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'
  ),
  array(
    'a' => 36,
    'b' => 'bv',
    'c' => '2015-08-28 00:00:00',
    'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'
  ),
  array(
    'a' => 12,
    'b' => 'xx',
    'c' => '2015-08-27 00:00:00',
    'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'
  )
);

处理要求:将数组中a相同的二维数组合并到一个数组中,生成一个新的数组

代码:

<?php
 $result= array();
 foreach ($infos as $key => $info) {
  $result[$info[&#39;a&#39;]][] = $info;
 }
 print_r($result);

输出:

Array
(
 [36] => Array
  (
   [0] => Array
    (
     [a] => 36
     [b] => xa
     [c] => 2015-08-28 00:00:00
     [d] => 2015/08/438488a00b3219929282e3652061c2e3.png
    )
   [1] => Array
    (
     [a] => 36
     [b] => se
     [c] => 2015-08-28 00:00:00
     [d] => 2015/08/438488a00b3219929282e3652061c2e3.png
    )
   [2] => Array
    (
     [a] => 36
     [b] => bv
     [c] => 2015-08-28 00:00:00
     [d] => 2015/08/438488a00b3219929282e3652061c2e3.png
    )
  )
 [3] => Array
  (
   [0] => Array
    (
     [a] => 3
     [b] => vd
     [c] => 2015-08-20 00:00:00
     [d] => 2015/08/438488a00b3219929282e3652061c2e3.png
    )
  )
 [6] => Array
  (
   [0] => Array
    (
     [a] => 6
     [b] => wwe
     [c] => 2015-08-28 00:00:00
     [d] => 2015/08/438488a00b3219929282e3652061c2e3.png
    )
   [1] => Array
    (
     [a] => 6
     [b] => aw
     [c] => 2015-08-28 00:00:00
     [d] => 2015/08/438488a00b3219929282e3652061c2e3.png
    )
  )
 [12] => Array
  (
   [0] => Array
    (
     [a] => 12
     [b] => xx
     [c] => 2015-08-27 00:00:00
     [d] => 2015/08/438488a00b3219929282e3652061c2e3.png
    )
  )
)

以上就是本文的全部内容,希望对大家的学习有所帮助。


相关推荐:

PHP在线调试执行的实现方法(附demo源码)_php技巧

php实现按天数、星期、月份查询的搜索框_php技巧

PHP实现的进度条效果详解_php技巧

Atas ialah kandungan terperinci php数组实现根据某个键值将相同键值合并生成新二维数组的方法详解. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn