I have an array like this
$filter_array = Array
(
[0] => Array
(
[fv_id] => 1
[fv_value] => Red
[filter_id] => 1
[filter_name] => Color
)
[1] => Array
(
[fv_id] => 2
[fv_value] => Blue
[filter_id] => 1
[filter_name] => Color
)
)
I would like to reduce the array by having filter_name and filter_id on top of array which is similar for all arrays.
$newArray = array_reduce($filter_array,function($carry,$item){
$allFilterValues[] = array(
'fv_id' => $item['fv_id'],
'fv_value' => $item['fv_value'],
);
$formated_array = array(
'filter_id' => $item['filter_id'],
'filter_name' => $item['filter_name'],
'filter_values' => $allFilterValues
);
return $formated_array;
});
But I am just getting the last array iterations value on filter_values
Array
(
[filter_id] => 1
[filter_name] => Color
[filter_values] => Array
(
[0] => Array
(
[fv_id] => 2
[fv_value] => Blue
)
)
)
But I want the array be like this.
Array
(
[filter_id] => 1
[filter_name] => Color
[filter_values] => Array
(
[0] => Array
(
[fv_id] => 1
[fv_value] => Red
),
[1] => Array
(
[fv_id] => 2
[fv_value] => Blue
)
)
)
On each iteration of
array_reduce
callback function must return current$carry
value:Update: if and only if in your
$filter_array
values of'filter_id'
and'filter_name'
are always the same you can simplify your code: