Last active
January 5, 2021 09:47
-
-
Save haxianhe/69c2ff8a289073894bead1c9fd1491b1 to your computer and use it in GitHub Desktop.
php数组按某一个元素值进行分组,正如数据库的group by语句
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
666