September 16, 2013
grouping multi-dimensional array in PHP
Devlin Carnate’s Question:
I have a multi-dimensional array, and I want to group it by org and then by dept. I have the first level of grouping working:
$groups = array();
foreach($inv_h as $item) {
$groups[$item['org']][] = $item;
}
How do I achieve the second level of grouping?
If thats how you are grouping the groups then:
$groups = array();
foreach($inv_h as $item) {
$groups[$item['org']][] = $item;
}
$depts = array();
foreach($inv_h as $item) {
$depts[$item['org']][] = $item;
}
// Then combine these two array
$merged = array_map(null, $groups, $depts);
// Now they are groups