If I was you, I would parse the JSON as an array instead of as an object. This can be done by doing the following:
$json = json_decode($data, true);
By included the second argument in json_decode with a value of true, you get back an array. You can then do something like:
echo '<pre>';
print_r($json);
exit;
This will give you an idea of the array structure of the data and how to access the information you want. For example, to pull the title, brand, and description of each item, you'd do the following:
foreach($json['items'] as $item) {
echo 'Title: ' . $item['product']['title'] . '<br />';
echo 'Brand: ' . $item['product']['brand'] . '<br />';
echo 'Description: ' . $item['product']['description'] . '<br />';
}
To get the availability, again do a dump of the array using print_r() and figure out the best way to access it from the array.
Answer from rahlstrom on Stack OverflowIf I was you, I would parse the JSON as an array instead of as an object. This can be done by doing the following:
$json = json_decode($data, true);
By included the second argument in json_decode with a value of true, you get back an array. You can then do something like:
echo '<pre>';
print_r($json);
exit;
This will give you an idea of the array structure of the data and how to access the information you want. For example, to pull the title, brand, and description of each item, you'd do the following:
foreach($json['items'] as $item) {
echo 'Title: ' . $item['product']['title'] . '<br />';
echo 'Brand: ' . $item['product']['brand'] . '<br />';
echo 'Description: ' . $item['product']['description'] . '<br />';
}
To get the availability, again do a dump of the array using print_r() and figure out the best way to access it from the array.
Your JSON is a mix of arrays and objects. So array notation will not work for all items. For example, to find the brand use:
foreach ($json->items as $item) {
var_dump($item->product->brand);
}
Codepad example
JSON specification
Videos
I have figured out half of what I am trying to do here.
I have a JSON array that is being returned via API. I am wanting to parse that array and pull out just the Unique ID and Email Address for each user.
Each user has a key called 'unique_id' regardless of whether or not it is defined. Each user only has a key called 'email' if a user in fact has an email address defined.
Sample Code:
foreach ($arr["data"]["users"] as $value) {
echo $value["unique_id"];
echo $value["email"];
}This code works....sort of. This will output the unique_id for each user. If there is no value defined, it simply returns a blank value. Cool. However, for the key email, I get an error: Undefined array key "email".
Am I getting this because not every user has this key?
You can see a sample of the array output here: https://pastebin.com/9D4mWyKL
It can be done with json_decode(), be sure to set the second argument to true because you want an array rather than an object.
$array = json_decode($json, true); // decode json
Outputs:
Array
(
[id] => 1
[name] => foo
[email] => [email protected]
)
Try json_decode:
$array = json_decode('{"id":1,"name":"foo","email":"[email protected]"}', true);
//$array['id'] == 1
//$array['name'] == "foo"
//$array['email'] == "[email protected]"
If you pass the JSON in your post to json_decode, it will fail. Valid JSON strings have quoted keys:
json_decode('{foo:"bar"}'); // this fails
json_decode('{"foo":"bar"}', true); // returns array("foo" => "bar")
json_decode('{"foo":"bar"}'); // returns an object, not an array.
Try this:
$data = json_decode($your_json_string, TRUE);
the second parameter will make decoded json string into an associative arrays.