我有两个数组,现在我想将它们组合起来,如下例所示:
$prices = array(100, 120, 135);
$discounts = array(5, 8, 15);
混合将离开:
$mixed_array = array(
array('prices' => 100 , 'discount' => 5),
array('prices' => 120 , 'discount' => 8),
array('prices' => 135 , 'discount => 15)
);
答案 0 :(得分:0)
这是一个在价格上使用foreach
循环的简单示例,它捕获索引或键以获得相应的折扣值。
$prices = array(100, 120, 135);
$discounts = array(5, 8, 15);
$mixed_array = array();
foreach($prices as $key => $price){
$mixed_array[] = array(
'prices' => $price,
'discount' => $discounts[$key]
);
}
输出
Array
(
[0] => Array
(
[prices] => 100
[discount] => 5
)
[1] => Array
(
[prices] => 120
[discount] => 8
)
[2] => Array
(
[prices] => 135
[discount] => 15
)
)