如何使用 foreach 迭代此 PHP 数组
How to iterate this PHP array using foreach
我使用此代码以数组形式从 API 获取数据:
$GetCurrentStock = $MyApi->GetCurrentStock('Array');
print_r($GetCurrentStock);
我正在获取数组形式的数据,如下所示:
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
我必须以编程方式将数据中的每个产品添加到我的数据库中。
单品添加如下:
$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);
如何使用 foreach 以编程方式批量添加所有这些产品?
您可以为您获得的所有记录创建一个foreach
循环。
$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);
}
我使用此代码以数组形式从 API 获取数据:
$GetCurrentStock = $MyApi->GetCurrentStock('Array');
print_r($GetCurrentStock);
我正在获取数组形式的数据,如下所示:
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
我必须以编程方式将数据中的每个产品添加到我的数据库中。 单品添加如下:
$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);
如何使用 foreach 以编程方式批量添加所有这些产品?
您可以为您获得的所有记录创建一个foreach
循环。
$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);
}