我從URL獲取總銷售物品,並返回包含'SKU','Date','Quantity'和'Total Price'的數組。 我想計算相同'SKU'的總數量並存儲在新陣列中。 任何人都可以告訴我如何在PHP語言中做到這一點?陣列與相同SKU的總和值
$url ="abc";
<pre>
$post_fields = array();
$post_fields['EndDateUtc']=$end;
$post_fields['StartDateUtc']=$start;
$post_fields['TenantToken']=$TenantToken;
$post_fields['UserToken']= $UserToken;
$data_string = json_encode($post_fields);
</pre>
<pre>
$curl_header = array();
$curl_header[] = 'Content-type: application/json';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $curl_header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 180);
curl_setopt($ch, CURLOPT_TIMEOUT, 180);
$output = curl_exec($ch);
curl_close($ch);
$datr = json_decode($output, TRUE);
print_r($datr);
陣列輸出: --------------
Array
(
[SoldItems] => Array
(
[0] => Array
(
[SKU] => 8273-Navy-M
[Date] => 2017-10-12
[Quantity] => 15
[TotalPrice] => 7685
)
[1] => Array
(
[SKU] => 2879-S-B-White-3XL
[Date] => 2017-10-12
[Quantity] => 2
[TotalPrice] => 298
)
[2] => Array
(
[SKU] => 8273-Navy-M
[Date] => 2017-10-12
[Quantity] => 2
[TotalPrice] => 298
)
[3] => Array
(
[SKU] => 2262-S-A-Black-2XL
[Date] => 2017-10-12
[Quantity] => 1
[TotalPrice] => 199
)
)
)
</pre>
thank you so much.
什麼 你有沒有嘗試過?請添加該代碼 –
任何嘗試呢?你嘗試過一個foreach()嗎? – Andreas
http://php.net/manual/en/function.array-column.php http://php.net/manual/en/function.array-map.php – Osama