Skip to content

Instantly share code, notes, and snippets.

@haxianhe
Last active January 5, 2021 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haxianhe/69c2ff8a289073894bead1c9fd1491b1 to your computer and use it in GitHub Desktop.
Save haxianhe/69c2ff8a289073894bead1c9fd1491b1 to your computer and use it in GitHub Desktop.
php数组按某一个元素值进行分组,正如数据库的group by语句
<?php
/**
* 数组按某一个元素值进行分组,正如数据库的group by语句
* @param $array
* @param $key
* @return array
*/
public function array_group_by($array, $key)
{
$res = [];
foreach ($array as $item) {
$res[$item[$key]][] = $item;
}
return $res;
}
@HulkHulk2345
Copy link

666

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment