group array based on value php code example
Example 1: group array php by key
private function group_by($array, $key) {
$return = array();
foreach($array as $val) {
$return[$val->$key][] = $val;
// ou para gettype($val) = array
// $return[$val[$key]][] = $val;
}
return $return;
}
Example 2: php group array by value and count
$colors=array("red","red","red","blue","green");
$colorsCount=array_count_values($colors);
print_r($colorsCount);
#Array
#(
# [red] => 3
# [blue] => 1
# [green] => 1
#)