我有一个这样的数组
$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
)
)
我想通过在数组顶部放置 filter_name和filter_id来减少数组,这对于所有数组都是相似的。
$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;
});
但我只是在filter_values上获得最后一个数组迭代值
Array
(
[filter_id] => 1
[filter_name] => Color
[filter_values] => Array
(
[0] => Array
(
[fv_id] => 2
[fv_value] => Blue
)
)
)
但我希望数组是这样的。
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
)
)
)