快乐每一天

自己选择的路,跪着也要走下去...

二维数组根据‘key’分组为新的三维数组

<?php
 
/*二维数组去掉重复值*/
 
$arr=array(
 '0' => 
  array (
    'wx_nickname' => '江淮智库',
    'wx_name' => 'anhui-sky',
    'url_times' => '0'
  ),
  '1' => 
  array (
    'wx_nickname' => '安徽发布',
    'wx_name' => 'anhuifabu', 
    'url_times' => '0'
  ),
  '2' => 
  array (
    'wx_nickname' => '安徽发布',
    'wx_name' => 'anhuifabu',
    'url_times' => '0'
  ),
  '3' => 
  array (
    'wx_nickname' => '江淮智库',
    'wx_name' => 'anhuifabu',
    'url_times' => '0'
  ),
  '4' => 
  array (
    'wx_nickname' => '安徽发布',
    'wx_name' => 'anhuifabu',
    'url_times' => '0'
  )
);
 
 
function array_group_by($arr, $key)
{
    $grouped = [];
    foreach ($arr as $value) {
        $grouped[$value[$key]][] = $value;
    }
    // Recursively build a nested grouping if more parameters are supplied
    // Each grouped array value is grouped according to the next sequential key
    if (func_num_args() > 2) {
        $args = func_get_args();
        foreach ($grouped as $key => $value) {
            $parms = array_merge([$value], array_slice($args, 2, func_num_args()));
            $grouped[$key] = call_user_func_array('array_group_by', $parms);
        }
    }
    return $grouped;
}
 
 
var_dump(array_group_by($arr, 'wx_nickname'));
?>
此文章共有条评论, 人参与 |Powerd By Angboo