PHP二维数组拆分

原创
2015/05/14 09:56
阅读数 671

二维数组如下:

$a = array(
  0 => array(
      'gindex' => '9',
      'report_type' => '3' 
	),
  1 => array(
      'gindex' => '9',
      'report_type' => '4'  
	),
  2 => array(
      'gindex' => '9', 
      'report_type' => '5'  
	),
  3 => array(
      'gindex' => '9',
      'report_type' => '6'  
	),
  4 => array(
      'gindex' => '25', 
      'report_type' => '2'  
	),
  5 => array(
      'gindex' => '25', 
      'report_type' => '3'  
	),
  6 => array(
      'gindex' => '26',
      'report_type' => '4'  
	),
  7 => array(
      'gindex' => '30',
      'report_type' => '2'  
	),
  8 => array(
      'gindex' => '30', 
      'report_type' => '3'  
	),
  9 => array(
      'gindex' => '417',
      'report_type' => '4'  
	)
);

想要的结果如下:

$a = array(
  9 => array(3,4,5,6),
  25 => array(2,3),
 .....
 )

实现代码如下:

  $arr = array();
  foreach ($a as $v) {
      $arr[$v['gindex']][] = $v['report_type'];
  }

以上提供一种思路,想要什么结构的数据,我们可以自行构造。

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部