2 回答
TA贡献1865条经验 获得超7个赞
您可以foreach像这样编辑数组并将结果存储在新数组中。
$items = array(
'key_1' => 'some value 1',
'key_2' => 'some value 2',
);
$result = array();
foreach ($items as $index => $value){
$id = str_replace('key_','',$index);
$result['array_'.$id] = [
'id' => $id,
'value' => $value,
];
}
extract($result); // must return array_1 and array_2
TA贡献1775条经验 获得超11个赞
好的,我已经这样做了:
foreach ($_POST as $key => $value) {
$array{$id[1]} = array();
$id = explode('_', $key);
${"array" . $id[1]}[] = array($id[0] => $value);
}
$array1 = call_user_func_array('array_merge', $array1);
var_dump($array1);
$array2 = call_user_func_array('array_merge', $array2);
var_dump($array2);
这会给我两个数组:
Array
(
[itemId] => 1
[product_id] => 1
[desc] => desc for the first item
[quantity] => 5
[unit] => kilogram
[priceUnit] => 100
[discount] => 0
[taxCost] => 45
)
Array
(
[itemId] => 2
[product_id] => 2
[desc] => desc for the second item
[quantity] => 10
[unit] => metre
[priceUnit] => 150
[discount] => 0
[taxCost] => 135
)
- 2 回答
- 0 关注
- 86 浏览
添加回答
举报