我在PHP中有这个数组。但我想提取数组的每个项目并将其放在外部数组中,如下所示:
[PordersProduct] => Array
(
[0] => Array
(
[1] => Array
(
[product_id] => 300001300
[Product] => Array
(
[name] => RELLENO CARNE P/ EMPANADA X KG
)
[requested] => 2.500
[formula_quantity] => 0
[cep_quantity] => 35.723
[wr_semi_finished_quantity] => 0
[in_execution_quantity] => 7488.131
[wr_total_semi_finished_quantity] => 7523.854
[to_produced_orders_quantity] => 0
)
)
[1] => Array
(
[0] => Array
(
[product_id] => 300002841
[Product] => Array
(
[name] => DISCO PARA EMPANDAS DE 85 GRS POR UNIDAD
)
[requested] => 50
[formula_quantity] => 0
[cep_quantity] => 0
[wr_semi_finished_quantity] => 0
[in_execution_quantity] => 7925
[wr_total_semi_finished_quantity] => 7925
[to_produced_orders_quantity] => 0
)
)
)
但我希望它是这样的:
[PordersProduct] => Array
(
[0] => Array
(
[product_id] => 300001300
[Product] => Array
(
[name] => RELLENO CARNE P/ EMPANADA X KG
)
[requested] => 2.500
[formula_quantity] => 0
[cep_quantity] => 35.723
[wr_semi_finished_quantity] => 0
[in_execution_quantity] => 7488.131
[wr_total_semi_finished_quantity] => 7523.854
[to_produced_orders_quantity] => 0
)
[1] => Array
(
[product_id] => 300002841
[Product] => Array
(
[name] => DISCO PARA EMPANDAS DE 85 GRS POR UNIDAD
)
[requested] => 50
[formula_quantity] => 0
[cep_quantity] => 0
[wr_semi_finished_quantity] => 0
[in_execution_quantity] => 7925
[wr_total_semi_finished_quantity] => 7925
[to_produced_orders_quantity] => 0
)
)
我该怎么做?
我试过这个:
grouped_data_porders2 = array(); // other array
foreach ($grouped_data_porders['PordersProduct'] as $key => $item) {
$grouped_data_porders2['PordersProduct'][] = $item[$key];
}
$grouped_data_porders['PordersProduct'] = $grouped_data_porders2['PordersProduct'];
但它不起作用,请你帮我。
由于
答案 0 :(得分:1)
你可以试试这个:
$data = [];
$newdata = [];
foreach($data as $item){
$newdata[] = $item[0];
}
更新:也请查看@ uri-goren的回答。它的概念相同但更短。
更新2: @ uri-goren似乎改变了他的回答。它是这样的:
$newdata = array_map(function($item){
return $item[0];
}, $data);
这是使用PHP的array_map函数,它遍历数组并将每个值发送到自定义函数并返回一个新数组,其中包含函数给出的值。它需要函数的名称或anonymous function以及一个或多个数组。