newcsv数组输出如下所示:
array(148) {
[0]=>
array(4) {
["parent_id"]=>
int(1554)
["id"]=>
int(1556)
["sku"]=>
string(11) "12000-Ash-S"
["qty"]=>
string(3) "199"
}
[1]=>
array(4) {
["parent_id"]=>
int(1554)
["id"]=>
int(1555)
["sku"]=>
string(11) "12000-Ash-M"
["qty"]=>
string(3) "691"
}
[2]=>
array(4) {
["parent_id"]=>
int(1554)
["id"]=>
int(1557)
["sku"]=>
string(11) "12000-Ash-L"
["qty"]=>
string(3) "740"
}
[3]=>
array(4) {
["parent_id"]=>
int(1554)
["id"]=>
int(1558)
["sku"]=>
string(12) "12000-Ash-XL"
["qty"]=>
string(3) "735"
}
}
$result = array();
foreach ($newcsv as $element) {
$result[$element['parent_id']][] = $element;
}
基于“parent\u id”输出对数组进行分组:
array(3) {
[1554]=>
array(48) {
[0]=>
array(4) {
["parent_id"]=>
int(1554)
["id"]=>
int(1556)
["sku"]=>
string(11) "12000-Ash-S"
["qty"]=>
string(3) "199"
}
[1]=>
array(4) {
["parent_id"]=>
int(1554)
["id"]=>
int(1555)
["sku"]=>
string(11) "12000-Ash-M"
["qty"]=>
string(3) "691"
}
[2]=>
array(4) {
["parent_id"]=>
int(1554)
["id"]=>
int(1557)
["sku"]=>
string(11) "12000-Ash-L"
["qty"]=>
string(3) "740"
}
}
[1603]=>
array(20) {
[0]=>
array(4) {
["parent_id"]=>
int(1603)
["id"]=>
int(1605)
["sku"]=>
string(13) "12300-Black-S"
["qty"]=>
string(4) "3000"
}
[1]=>
array(4) {
["parent_id"]=>
int(1603)
["id"]=>
int(1604)
["sku"]=>
string(13) "12300-Black-M"
["qty"]=>
string(4) "3000"
}
[2]=>
array(4) {
["parent_id"]=>
int(1603)
["id"]=>
int(1606)
["sku"]=>
string(13) "12300-Black-L"
["qty"]=>
string(4) "3000"
}
}
[1624]=>
array(80) {
[0]=>
array(4) {
["parent_id"]=>
int(1624)
["id"]=>
int(1626)
["sku"]=>
string(13) "12500-White-S"
["qty"]=>
string(4) "1858"
}
[1]=>
array(4) {
["parent_id"]=>
int(1624)
["id"]=>
int(1625)
["sku"]=>
string(13) "12500-White-M"
["qty"]=>
string(4) "2295"
}
[2]=>
array(4) {
["parent_id"]=>
int(1624)
["id"]=>
int(1627)
["sku"]=>
string(13) "12500-White-L"
["qty"]=>
string(4) "1974"
}
}
}
$data = [ 'update' => [
[
'id' => 733,
'sku' => '344'
],
[
'id' => 733,
'sku' => '200'
]
]
];
我使用下面的代码创建数组。但这对我没用。
$data = array();
foreach($result as $key => $value){
$data = ('update' =>array('id'=>$value['id'],'regular_price'=>$value['sku']));
echo"<pre>";
print_r($data);
echo"</pre>";
}