<?php // 封装分组逻辑的函数 function groupBy($array, $key) { $result = []; foreach ($array as $element) { $result[$element[$key]][] = $element; } $new=[]; foreach($result as $k=>$v){ $new[$k]['ww']=$v[0]; $new[$k]['children']=$v; } return $new; } // 原始用户数组 $users = [ ['name' => 'Alice', 'age' => 23, 'email' => '[email protected]', 'gender' => 'female'], ['name' => 'Bob', 'age' => 30, 'email' => '[email protected]', 'gender' => 'male'], ['name' => 'Charlie', 'age' => 25, 'email' => '[email protected]', 'gender' => 'male'], ['name' => 'David', 'age' => 30, 'email' => '[email protected]', 'gender' => 'male'], ]; // 按照年龄分组 $groupedUsers = groupBy($users, 'age'); print_r($groupedUsers); exit();
最完美的分组方案
标签:group,name,gender,age,cat,example,mysql,com,email From: https://www.cnblogs.com/newmiracle/p/18470848