php 中的 array_merge 问题

我在使用该函数处理数组时遇到了一些问题array_merge。这是一个例子:


$first_array = [

    8 => [

        'name' => "Hershey's Chocolate Milk Shake",

        'code' => 8 ,

        'price' => 29.00,

        'quantity' => 1,

        'image' => "Hersheys_Chocolate_Milk_Shake.jpg",

        'percentage_discount' => 0,

        'offer_mrp' => 0,

    ]

];

$second_array = [

    20 => [

        'name' => 'Kissan Mixed Fruit Jam 700g',

        'code' => 20,

        'price' => 215.00,

        'quantity' => 1,

        'image' => 'Kissan Mixed Fruit Jam 700g.jpg',

        'percentage_discount' => 0,

        'offer_mrp' => 0 

    ]

];


$first_array = array_merge($first_array, $second_array); 

print_r($first_array);

结果是:


Array ( 

    [0] => Array ( 

        [name] => Hershey's Chocolate Milk Shake 

        [code] => 8 

        [price] => 29.00 

        [quantity] => 1 

        [image] => Hersheys_Chocolate_Milk_Shake.jpg 

        [percentage_discount] => 0 

        [offer_mrp] => 0

    ) 

    [1] => Array ( 

        [name] => Kissan Mixed Fruit Jam 700g 

        [code] => 20 

        [price] => 215.00 

        [quantity] => 1 

        [image] => Kissan Mixed Fruit Jam 700g.jpg 

        [percentage_discount] => 0 [offer_mrp] => 0 

    ) 

);

但我希望它是:


Array ( 

    [8] => Array ( 

        [name] => Hershey's Chocolate Milk Shake 

        [code] => 8 

        [price] => 29.00 

        [quantity] => 1 

        [image] => Hersheys_Chocolate_Milk_Shake.jpg 

        [percentage_discount] => 0 

        [offer_mrp] => 0

    ) 

    [20] => Array ( 

        [name] => Kissan Mixed Fruit Jam 700g 

        [code] => 20 

        [price] => 215.00 

        [quantity] => 1 

        [image] => Kissan Mixed Fruit Jam 700g.jpg 

        [percentage_discount] => 0 [offer_mrp] => 0 

    )

)


慕斯王
浏览 120回答 1
1回答

犯罪嫌疑人X

array_merge()重新枚举数字键。您应该改用运算+符。$first_array = $first_array + $second_array;输出与您想要的完全相同。
打开App,查看更多内容
随时随地看视频慕课网APP