In the following JSON, I want to fetch all the customers phone and store in a array:
{
"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"
}
]
}
I tried using following but it keeps throwing me error "Trying to get property of non-object in":
$customer_json = json_decode($api->getAllCustomers());
$customer_json->Customers[0]->Phone;
The problem is that PHP representation of JSON object is not an object. PHP uses associative arrays instead. For convinient behavior use:
$customer_json = json_decode($api->getAllCustomers(), true);
$customer_json['Customers'][0]['Phone'];