-2
我有下面的代碼的問題:PHP - 如何通過JSON數組JSON陣列內重複
$invoice_customers = [];
foreach ($new['data'] as $key=>$display) {
if ($display['customer'] == $customer){
$invoice_customers[$key]['customer'] = $display['source']['customer'];
$invoice_customers[$key]['name'] = $display['source']['name'];
$invoice_customers[$key]['amount'] = $display['amount'];
$invoice_customers[$key]['refunded'] = $display['refunded'];
$invoice_customers[$key]['last4'] = $display['source']['last4'];
$invoice_customers[$key]['id'] = $display['id'];
$invoice_customers[$key]['created'] = $display['created'];
}
}
echo json_encode($invoice_customers);
這將返回:
{"3":
{"customer":"xxxxx","name":"Testtt Test","amount":100,"refunded":false,"last4":"4242","id":"x","created":1500342123},
"4xxx":
{"customer":"xxxxx","name":"Testtt Test","amount":100,"refunded":false,"last4":"4242","id":"xxxx","created":1500340979}}
正如你可以看到我有主陣列內的一些數組。我想知道如何只訪問主數組中的數組? IE:事情是這樣的:
{"customer":"xxxxx","name":"Testtt Test","amount":100,"refunded":false,"last4":"xxxx","id":"x","created":1500342123},
{"customer":"xxxxx","name":"Testtt Test","amount":100,"refunded":false,"last4":"xxxx","id":"xxxx","created":1500340979}
你必須再次解碼該入分級陣列,然後迭代外數組的元素。或者訪問或搜索特定的元素,只要你喜歡。 – arkascha
使用json_decode獲取一個json對象,然後能夠遍歷它像$ invoice_customers - > [「3」] - >名稱 – LordNeo
您構建一個PHP數據結構,您將其編碼爲JSON,然後您想要什麼?您已擁有'$ invoice_customers'中的所有內容。你爲什麼要*「在主數組中訪問數組」*在一個字符串中? – axiac