As per the documentation, you need to specify true as the second argument if you want an associative array instead of an object from json_decode. This would be the code:
$result = json_decode($jsondata, true);
If you want integer keys instead of whatever the property names are:
$result = array_values(json_decode($jsondata, true));
However, with your current decode you just access it as an object:
print_r($obj->Result);
Answer from Stephen on Stack OverflowAs per the documentation, you need to specify true as the second argument if you want an associative array instead of an object from json_decode. This would be the code:
$result = json_decode($jsondata, true);
If you want integer keys instead of whatever the property names are:
$result = array_values(json_decode($jsondata, true));
However, with your current decode you just access it as an object:
print_r($obj->Result);
try this
$json_string = 'http://www.domain.com/jsondata.json';
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata,true);
echo "<pre>";
print_r($obj);
Videos
$string = file_get_contents('./string.json');
$json = json_decode($string);
if you want to have items: <address>:
foreach ($json['items'] as $address)
{
echo "items:". $address['address'] ."\n";
};
anyway if you are not sure about how an array is built you could print it via:
print_r($json);
which will print:
Array
(
[items] => Array
(
[0] => Array
(
[address] => W 7th Ave
)
[1] => Array
(
[address] => W 8th St
)
)
)
now you found out that $json contains just an array (items) of two array, then, if you loop it, you will get that array which is printed in your example.
As explained above you need to go one step deeper by looping the elements in your items array and print their address element.
here is the complete script: http://pastie.org/2275879
Your items are in an array. You could loop through them like this:
foreach ($json['items'] as $address)
{
echo 'Address: '.$address;
}
This might get closed as opinion based, but for me, I would typically decode to whatever data structure makes the most sense for the use case.
For example, say the JSON described a single item like a book and looked something like this:
{
"title": "Cool Book",
"author": "Amazing Author",
"publisher": "Evil Corporation",
...
}
To me that is an object, in that it is a single item with different properties. I would be likely to want to treat it like an object in my subsequent code, so I would decode it as an object.
Now if the JSON contained the sort of data that may represent a dictionary, map, hash table, etc. sort of structure, where all the key-value pairs were, in essence, similar items, just with different lookups and mapped values, I might consider decoding to an associative array. Maybe a good example of that would be a country code to country name map like this:
{
"AF": "Afghanistan",
"AX": "Aland Islands",
"AL": "Albania",
"DZ": "Algeria",
...
}
I might be inclined to decode this to an associative array, because I don't need any object-oriented representation of this information, since I am just using this for key-value lookups.
To answer your question about other data structures that can be represented in JSON, officially there are only two data structures supported in JSON - objects and numerically-indexed arrays. This is because of the javascript-based roots of the serialization format, where, for example, the concept of an "out-of-the-box" associative array doesn't exist.
You will however find that a number of JSON encoding/decoding libraries across languages do add support for other data structures or types, typically adding handling behavior around primitive data types, but I would not rely on this unless you fully understand the data structures that are going to be passed and how they are going to be encoded/decoded across all applications that might pass the data around.
For example, PHP provides support for certain primitives as shown in this note from the json_encode() documentation:
Note: Like the reference JSON encoder, json_encode() will generate JSON that is a simple value (that is, neither an object nor an array) if given a string, integer, float or boolean as an input value. While most decoders will accept these values as valid JSON, some may not, as the specification is ambiguous on this point. To summarise, always test that your JSON decoder can handle the output you generate from json_encode().
Finally, with regards to performance, if you get to the point in your application development where the number one concern is optimizing performance for execution time, memory utilization, etc. and you have reason to believe that relatively substantial gains can be made by optimizing the JSON deserialization (and subsequent data access) logic, then you should ultimately test your application with representative data and see what works best for you. My guess is that this would be along the lines of a micro-optimization for most applications.
I think the reason is, that JSON is "JavaScript Object Notation" and therefore people expect an object. If you look at json.org the an object is defined as an unordered set of name/value pairs and an array is for ordered collections of values, like people are used to it in javascript. This can also be found in the RFC 4627:
The terms "object" and "array" come from the conventions of JavaScript.