在一个多维数组中混合两个数组



我有两个数组,现在我想将它们组合为以下示例:

$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)
);

这是一个对价格使用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
        )
)

相关内容

  • 没有找到相关文章