php - How to convert single array to multidimensional and group base on value name
one text
Solution:
You can use to group your array based on the
group_name
and filter_group_id
values, making a key for the result array based on those two values and adding the matching filter values to the filters
subarray for each key. You can then use to re-index the result array numerically:
$result = array_reduce($data, function ($c, $v) {
$key = $v['group_name'] . '#' . $v['filter_group_id'];
if (!isset($c[$key])) {
$c[$key] = array('group_name' => array('group_name' => $v['group_name'], 'filter_group_id' => $v['filter_group_id']));
}
$c[$key]['filters'][] = array('filter_id' => $v['filter_id'], 'name' => $v['name']);
return $c;
}, array());
$result = array_values($result);
print_r($result);
The output is too long to show here but matches your requirement.
Source