我有一个csv上传者我正在创建推送到订单api。使用phpSpreadsheets toArray
方法,我有一个像这样的数组:
Array
(
[0] => Array
(
[0] => product_sku
[1] => product_qty
[2] => shipping_name
[3] => shipping_address1
[4] => shipping_address2
[5] => shipping_city
[6] => shipping_county
[7] => shipping_postcode
[8] => shipping_type
[9] => customer_id
)
[1] => Array
(
[0] => test_sku_1
[1] => 3
[2] => Bruce Wayne
[3] => The Manor
[4] => Near Arkahm Asylumn
[5] => Gotham
[6] => Greater Gothan
[7] => B17MAN
[8] => 1
[9] => 14994333
)
[2] => Array
(
[0] => test_sku_2
[1] => 2
[2] => Bruce Wayne
[3] => The Manor
[4] => Near Arkahm Asylumn
[5] => Gotham
[6] => Greater Gothan
[7] => B17MAN
[8] => 1
[9] => 14994333
)
[3] => Array
(
[0] => test_sku_3
[1] => 7
[2] => Bruce Wayne
[3] => The Manor
[4] => Near Arkahm Asylumn
[5] => Gotham
[6] => Greater Gothan
[7] => L17MA2
[8] => 1
[9] => 14994333
)
)
每个订单都在新行上,但是如果客户订购了两个不同的商品,我需要使用他们的邮政编码将它们组合在一起。因此,我的目标是重新排列这种格式:
[orders] => Array(
Array(
[shipping_name] => "Bruce wayne",
[customer_id] => 14994333,
[address] => Array(
[shipping_address1] => "The Manor",
[shipping_address2] => "Near Arham Asylumn",
[shipping_city] => "Gotham",
[shipping_county] => "Greater Gotham",
[shipping_postcode] => "B17MAN",
)
[products] => Array(
Array(
[sku] => "test_sku_1",
[quantity] => 3
),
Array(
[sku] => "test_sku_2",
[quantity] => "2"
)
)
)
)
我遇到的第一个问题是尝试匹配邮政编码。我设法使用:
$getDuplicates = array_count_values(array_map(function($duplicates) {
return $duplicates[7]; //Where 7 is the postcode
}, $rows));
这有效地计算了我需要的东西。然而,从那里我撞到了一堵砖墙。如果我正在计算重复项,我还需要记下它已经通过的行,这样它们就不会被错误地推入我想要制作的新数组中。
伪它应该是:
for each rows as row{
if row isn't set to be ignored{
for each count of this postcode{
array_push the product sku and qty
mark these rows as rows to be ignored
}
}
}
任何人都可以帮我吗?
答案 0 :(得分:0)
所以,长话短说,我已经陷入了这种情况,我完全错过了显而易见的事实。为了解决这个问题,我从phpspreadsheet中获取了原始数组并执行了array_multisort
:
foreach ($rows as $key => $row) {
$postcodes[$key] = $row[7]; //the postcode key
}
array_multisort($postcodes, SORT_DESC, $rows;
然后我只是在数组中工作,如果邮政编码发生变化,我就会创建一个新数组,否则我只需直接进入产品的正确数组。
我觉得自己真的很蠢,以前我没想过这个。感谢@apokryfos试图提供帮助!