我有两个数组,一个是产品标题,另一个是数量。我需要将它们存储在一个新数组中,该数组的第一个位置为“ title”和“ quantity”。
这就是我的想法:
//arrayTitle and arrayQuantity already come with stored data, both arrays of the same length
$newArray[];
for($i=0;$i<count($arrayTitle);$i++){
$newArray = array("title" => value of the position $i of the title array,
"quantity" => value of the position $i of the quantity array);
}
答案 0 :(得分:1)
尝试使用foreach循环,例如:
foreach ($arrayTitle as $i => $title) {
$newArray[] = [
'title' => $title,
'quantity' => $arrayQuantity[$i]
];
}
答案 1 :(得分:1)
只需将它们映射到以数组格式返回它们的回调:
$newArray = array_map(function($t, $q) {
return array("title" => $t, "quantity" => $q);
}, $arrayTitle, $arrayQuantity);