将值添加到数组中

问题描述 投票:0回答:1

我有一个数组$products

Array
    (
        [1000] => Array
            (
                [key] => 1000
                [name] => Cat
            )

    )

        [2000] => Array
            (
                [key] => 2000
                [name] => Dog
            )

    )

并且我通过服务($extProducts)检索外部数组,我想修改该数组($products),使其结构如下:

Array
   (
       [1000] => Array
           (
               [key] => 1000
               [name] => Cat
               [ID] => 77
           )

   )

       [2000] => Array
           (
               [key] => 2000
               [name] => Dog
               [ID] => 9

           )

   )

我尝试了以下操作:

foreach ($extProducts as $extProduct){
   $product['ID'] = $extProduct["ID"];
   array_push($products, $product);
}

这只是完全改变了数组,没有给我我所需要的,任何人都可以帮忙。

谢谢

php
1个回答
0
投票
foreach ($extProducts as $id=>$extProduct){
   $extProducts[$id]["ID"]=123; //reference your ID here
}

希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.