[英]How to iterate JSON in PHP
在以下JSON中,我想獲取所有客戶電話並存儲在數組中:
{
"MetaInformation":{
"@TotalResources":1,
"@TotalPages":1,
"@CurrentPage":1
},
"Customers":[
{
"@url":"https:\/\/URL/customers\/1",
"Address1":"Mumbai",
"City":"Mumbai",
"CustomerNumber":"1",
"Email":"xyz@gmail.com",
"Name":"Saurabh Pradhan",
"OrganisationNumber":"",
"Phone":"91xxx",
"ZipCode":"45153"
}
]
}
我嘗試使用以下方法,但始終使我拋出錯誤“嘗試獲取非對象的屬性”:
$customer_json = json_decode($api->getAllCustomers());
$customer_json->Customers[0]->Phone;
問題在於,JSON對象的PHP表示形式不是對象。 PHP使用關聯數組代替。 為方便起見,請使用:
$customer_json = json_decode($api->getAllCustomers(), true);
$customer_json['Customers'][0]['Phone'];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.