企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## groupBy 根据指定的分组函数对值进行分组 例子: ~~~ $members=[ ['id'=>1,'name'=>'小明','sex'=>'男','age'=>18,'register_time'=>'2017-04-01 08:30:01'], ['id'=>2,'name'=>'小红','sex'=>'女','age'=>18,'register_time'=>'2017-04-02 12:21:32'], ['id'=>3,'name'=>'小刚','sex'=>'男','age'=>16,'register_time'=>'2017-04-05 09:10:13'], ['id'=>4,'name'=>'小丽','sex'=>'女','age'=>19,'register_time'=>'2017-04-05 12:05:27'], ['id'=>5,'name'=>'小壮','sex'=>'男','age'=>21,'register_time'=>'2017-04-08 15:41:59'], ['id'=>6,'name'=>'小东','sex'=>'男','age'=>21,'register_time'=>'2017-04-01 08:30:01'], ]; $dataSource=Traversable::from($members); $result=$dataSource->groupBy(function($m){ return $m['age']; //按照age分组,age相同的分到同一组。 }); foreach ($result as $s){ dump($s); } ~~~ 打印结果: ~~~ Traversable {#45 ▼ #scheme: GeneratorScheme {#17} #source: Traversable {#14 ▶} #elements: OrderedMap {#34 ▼ #keys: array:2 [▶] #values: array:2 [▼ 0 => &2 array:5 [▼ "id" => 1 "name" => "小明" "sex" => "男" "age" => 18 "register_time" => "2017-04-01 08:30:01" ] 1 => &3 array:5 [▼ "id" => 2 "name" => "小红" "sex" => "女" "age" => 18 "register_time" => "2017-04-02 12:21:32" ] ] #keyIdentityPositionMap: array:2 [▶] #length: 2 #largestIntKey: 1 } } Traversable {#49 ▼ #scheme: GeneratorScheme {#17} #source: Traversable {#14 ▶} #elements: OrderedMap {#36 ▼ #keys: array:1 [▶] #values: array:1 [▼ 0 => &4 array:5 [▼ "id" => 3 "name" => "小刚" "sex" => "男" "age" => 16 "register_time" => "2017-04-05 09:10:13" ] ] #keyIdentityPositionMap: array:1 [▶] #length: 1 #largestIntKey: 2 } } Traversable {#45 ▼ #scheme: GeneratorScheme {#17} #source: Traversable {#14 ▶} #elements: OrderedMap {#37 ▼ #keys: array:1 [▶] #values: array:1 [▼ 0 => &5 array:5 [▼ "id" => 4 "name" => "小丽" "sex" => "女" "age" => 19 "register_time" => "2017-04-05 12:05:27" ] ] #keyIdentityPositionMap: array:1 [▶] #length: 1 #largestIntKey: 3 } } Traversable {#49 ▼ #scheme: GeneratorScheme {#17} #source: Traversable {#14 ▶} #elements: OrderedMap {#38 ▼ #keys: array:2 [▶] #values: array:2 [▼ 0 => &6 array:5 [▼ "id" => 5 "name" => "小壮" "sex" => "男" "age" => 21 "register_time" => "2017-04-08 15:41:59" ] 1 => &7 array:5 [▼ "id" => 6 "name" => "小东" "sex" => "男" "age" => 21 "register_time" => "2017-04-01 08:30:01" ] ] #keyIdentityPositionMap: array:2 [▶] #length: 2 #largestIntKey: 5 } } ~~~