我訪問一個JSON解碼陣列遍歷數組的特定按鍵與PHP
$orders = json_decode($response, true);
然後被叫數據的陣列中,我會通過每個訂單並找到總鍵。
// access the common array
$orders = $orders['data'];
// access the total of each order
$sale1 = $orders[0]['total'];
$sale2 = $orders[1]['total'];
$sale3 = $orders[2]['total'];
$sale4 = $orders[3]['total'];
$sale5 = $orders[4]['total'];
$sale6 = $orders[5]['total'];
$sale7 = $orders[6]['total'];
然後我把它們加起來得到所有訂單的總和。
// add up all orders to find total
echo $sale1 + $sale2 + $sale3 + $sale4 + $sale5 + $sale6
我不能弄清楚如何將它變成一個循環。
這裏是我的數組:
array(3) {
[0]=>
array(53) {
["total"]=>
int(100)
}
[1]=>
array(53) {
["total"]=>
int(100)
}
}