drxm72811 2015-04-01 13:07
浏览 60
已采纳

如何使用foreach迭代这个PHP数组

I use this code to get data from API in form of Array:

$GetCurrentStock = $MyApi->GetCurrentStock('Array');
 print_r($GetCurrentStock);

I am getting data in the form of Array like this:

Array ( [Status] => Ok [ReturnVal] => Array ( [0] => Array ( [name] => Alcatel [model] => Hero OT-8020X [color] => Black [warehouse] => HU11 [bar_code] => A20200125 [in_stock] => <20 [exp_delivery] => 0 [exp_available] => <20 [delivery_date] => - [price] => 301.90 [properties] => Array ( [eu_warranty] => no [keypad] => Touch screen [manual] => Hun [simlock] => Sim Free [remarks] => Data cable, headset [language] => ger, eng, esp, fra, ita, hun, ned, por, rom, tur [country] => China ) [ean] => [image] => http://www.mobileshop.bz/phone-pictures/api/3531-alcatel-hero-ot-8020x.jpg [id] => 3531 [category] => mobile ) [1] => Array ( [name] => Alcatel [model] => Idol 2 Mini OT-6016X [color] => Gray [warehouse] => HU11 [bar_code] => A20200121 [in_stock] => <5 [exp_delivery] => 0 [exp_available] => <5 [delivery_date] => - [price] => 192.60 [properties] => Array ( [eu_warranty] => no [keypad] => Touch screen [manual] => Hun [simlock] => Sim Free [remarks] => Data cable, headset [language] => cat, ger, eng, esp, fra, ita, hun, ned, por, rom, tur [country] => China ) [ean] => [image] => http://www.mobileshop.bz/phone-pictures/api/3345-alcatel-idol-2-mini-ot-6016x.jpg [id] => 3345 [category] => mobile ) [2] => Array ( [name] => Alcatel [model] => Idol 2 Mini OT-6016X [color] => White [warehouse] => HU11 [bar_code] => A20200120 [in_stock] => <5 [exp_delivery] => 0 [exp_available] => <5 [delivery_date] => - [price] => 192.60 [properties] => Array ( [eu_warranty] => no [keypad] => Touch screen [manual] => Hun [simlock] => Sim Free [remarks] => Data cable, headset [language] => cat, ger, eng, esp, fra, ita, hun, ned, por, rom, tur [country] => China ) [ean] => [image] => http://www.mobileshop.bz/phone-pictures/api/3346-alcatel-idol-2-mini-ot-6016x.jpg [id] => 3346

I have to add each product from the data programmatically to my database. A single product is added like this:

 $productData = array(
        'product_description' => array('1' => array('name' => 'Alcatel', 'meta_description' => '' ,'meta_keyword' =>'', 'description' => '', 'tag' =>'')),

        'model' => 'Alcatel Hero OT-8020X Black',
        'price' => '301.40',

        'tax_class_id' => 0,
        'quantity' => 1,
        'minimum' => 1,
        'subtract' => 1,
        'stock_status_id' => 6,
        'shipping' => 1 ,
        'image' => 'http://www.mobileshop.bz/phone-pictures/api/3531-alcatel-hero-ot-8020x.jpg',
        'manufacturer' => 'Alcatel',
        'manufacturer_id' => 44,
        'category' => 'ce',
        'product_category' => array('0' => 61),
        'product_store' => array('0' => 0),
        'date_available' => '2015-03-31',
        'length_class_id' => 1,
        'weight_class_id' => 1,
        'status' => 1,
        'sort_order' => 1,

        );

    //load model
    $this->load->model('catalog/product');

    // Attempt to pass the assoc array to the add Product method
    $this->model_catalog_product->addProduct($productData);

How do I add all those product in bulk programmatically using foreach?

  • 写回答

1条回答 默认 最新

  • douniani679741 2015-04-01 13:27
    关注

    You can create a foreach loop for all the records you have got.

    $this->load->model('catalog/product');
    
    foreach($GetCurrentStock['ReturnVal'] as $value){
        $productData = array(
            'model' => $value['model'],
            'price' => $value['price'],
        );
    
      // add other values in above array 
      $this->model_catalog_product->addProduct($productData);
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档
  • ¥50 C++五子棋AI程序编写
  • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。