json_decode works for me on your data:
print_r(json_decode('{
"data": [
{
"name": "Joe Bloggs",
"id": "203403465"
},
{
"name": "Fred Bloggs",
"id": "254706567"
},
{
"name": "Barny Rubble",
"id": "453363843"
},
{
"name": "Homer Simpson",
"id": "263508546"
}
]
}
', true));
Output:
Array
(
[data] => Array
(
[0] => Array
(
[name] => Joe Bloggs
[id] => 203403465
)
[1] => Array
(
[name] => Fred Bloggs
[id] => 254706567
)
[2] => Array
(
[name] => Barny Rubble
[id] => 453363843
)
[3] => Array
(
[name] => Homer Simpson
[id] => 263508546
)
)
)
Setting the second argument to true returns an associative array.
JSON to PHP Associative array - Stack Overflow
php - json_decode: object to associative array - Stack Overflow
json - PHP json_decode to associative array - Stack Overflow
php - Use json_decode() to create array insead of an object - Stack Overflow
How do I convert JSON to an array in PHP?
Why does json_decode return null?
How do I read a JSON file in PHP?
json_decode works for me on your data:
print_r(json_decode('{
"data": [
{
"name": "Joe Bloggs",
"id": "203403465"
},
{
"name": "Fred Bloggs",
"id": "254706567"
},
{
"name": "Barny Rubble",
"id": "453363843"
},
{
"name": "Homer Simpson",
"id": "263508546"
}
]
}
', true));
Output:
Array
(
[data] => Array
(
[0] => Array
(
[name] => Joe Bloggs
[id] => 203403465
)
[1] => Array
(
[name] => Fred Bloggs
[id] => 254706567
)
[2] => Array
(
[name] => Barny Rubble
[id] => 453363843
)
[3] => Array
(
[name] => Homer Simpson
[id] => 263508546
)
)
)
Setting the second argument to true returns an associative array.
You have to make a new array
$json_array = json_decode($_POST['json'], true);
$assoc_array = array();
for(
i < sizeof($json_array); $i++)
{
$key = $json_array[$i]['name'];
$assoc_array[
json_array[$i]['value'];
}
you will get your associative array in $assoc_array and you can now directly access using indexes.
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);
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);
have you tried json_decode($test, true) ?
You can give set a second Parameter. When TRUE, returned objects will be converted into associative arrays. http://php.net/manual/en/function.json-decode.php
<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
var_dump(json_decode($json, true));
?>
output:
object(stdClass)#1 (5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
array(5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}