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);
php - How to json_decode an array of objects? - Stack Overflow
decode json array with multiple objects in php - Stack Overflow
php json decode object with array of multiple objects - Stack Overflow
How can I get JSON objects by array?
json_decode() does so work. The second param turns the result in to an array:
var_dump(json_decode('[{"a":"b"},{"c":"d"},{"e":"f"}]', true));
// gives
array(3) {
[0]=>
array(1) {
["a"]=>
string(1) "b"
}
[1]=>
array(1) {
["c"]=>
string(1) "d"
}
[2]=>
array(1) {
["e"]=>
string(1) "f"
}
}
$array = '[{"a":"b"},{"c":"d"},{"e":"f"}]';
print_r(json_decode($array, true));
Read the manual - parameters for the json_decode method are clearly defined:
http://www.php.net/manual/en/function.json-decode.php
I tried following code on PHP 5.6.11-1ubuntu3.1 (cli), and surprisingly got exactly what I wanted. Since this question is so old, I don't remember what was the PHP version when I tried this at the time of posting this code.
Code:
<?php
class Test {
public $message = "this is message";
public $number = 10;
}
$t1 = new Test();
$t2 = new Test();
$arr = array(0 => $t1, 1 => $t2);
$json_data = json_encode($arr);
var_dump($json_data);
$original_array = json_decode($json_data, true);
$original_object = json_decode($json_data, false);
var_dump($original_array);
var_dump($original_object);
?>
The output I get is:
string(85) "[{"message":"this is message","number":10},{"message":"this is message","number":10}]"
array(2) {
[0]=>
array(2) {
["message"]=>
string(15) "this is message"
["number"]=>
int(10)
}
[1]=>
array(2) {
["message"]=>
string(15) "this is message"
["number"]=>
int(10)
}
}
array(2) {
[0]=>
object(stdClass)#3 (2) {
["message"]=>
string(15) "this is message"
["number"]=>
int(10)
}
[1]=>
object(stdClass)#4 (2) {
["message"]=>
string(15) "this is message"
["number"]=>
int(10)
}
}
Try this :
$arr = array(0 => "o1", 1 => "o2");
$json_data = json_encode($arr);
$res = json_decode($json_data, true);
echo "<pre>";
print_r($res);
Output :
Array
(
[0] => o1
[1] => o2
)
Give this a try:
$json = '
{
"article_details":[
{
"article_code ":"000000000300028156 ",
"diff_amnt ":"1 "
},
{
"article_code ":"000000000300028174 ",
"diff_amnt ":"1 "
},
{
"article_code ":"000000000300028126 ",
"diff_amnt ":"1 "
}
],
"article":[
{
"article_code ":"000000000300028156 ",
"diff_amnt ":"1 "
},
{
"article_code ":"000000000300028174 ",
"diff_amnt ":"1 "
},
{
"article_code ":"000000000300028126 ",
"diff_amnt ":"1 "
}
]
}';
$key = 'article_code '; // Since your key is 'article_code ' but not 'article_code', I have to do this
$dataObject = json_decode($json);
echo $dataObject->article_details[0]->$key; // This will out put: 000000000300028156
$dataArray = json_decode($json, true);
echo $dataArray['article_details'][0]['article_code ']; // This will out put: 000000000300028156
Use json_decode() to decode a JSON string, like this:
$json_array = json_decode($json, true);
^ When TRUE, returned objects will be converted into associative arrays.
So your code should be like this:
// Here $json is your json string
$json_array = json_decode($json, true);
foreach($json_array as
arrays){
echo $key . "<br />";
foreach($arrays as $array){
foreach($array as
value){
echo
value . "<br />";
}
}
echo "<br />";
}
Output:
article_details
article_code => 000000000300028156
diff_amnt => 1
article_code => 000000000300028174
diff_amnt => 1
article_code => 000000000300028126
diff_amnt => 1
article
article_code => 000000000300028156
diff_amnt => 1
article_code => 000000000300028174
diff_amnt => 1
article_code => 000000000300028126
diff_amnt => 1
$myArray = json_decode($json, true);
var_dump($myArray['id']);
var_dump($myArray['user'][0]['fname']);
You can decode these values from json to object.
$result = json_decode('{"id":"jai","pwd":"123","user":[{"fname":"jai","lname":"gupta"},{"fname":"sameer","lname":"seth"}],"college":"vit"}');
and acccess like below:
$result->id;
$result->pwd;
Hello, I’m not certain how to word this question and it not be confusing so I’ll just provide the use case in hopes someone can lend a solution.
I have a json file and each object contains an ID element that is a number.
In the database, I could have something like 2,17,25.
How would I go about selecting those items into an associated php array?
So currently I’m using a foreach loop to output the entire thing, and I suppose I could use in_array() inside of the loop, but that seems inefficient as it is a large json with a few hundred entries and that would mean a few hundred in_array calls. Perhaps some kind of array diff call before the foreach loop maybe? But I’m not sure how to implement that when the json array is key value and I have only the comma delimited string. Any help appreciated.
The second parameter of json_decode determines whether to return the result as an array instead of an object. Since you set it to true your result is an array and not an object.
$content = $final_res['articles'][0]['Content'];
As an alternative answer, if you want to use it as an object, use this code:
$a = '{"articles":[{"ID":"1","Title":"Welcome","Content":"This is the first article."}]}';
$final_res = json_decode($a);
echo '<pre>';
print_r($final_res);
echo '</pre><br>';
Note that I removed the second part (true) from the json_decode
Output:
stdClass Object
(
[articles] => Array
(
[0] => stdClass Object
(
[ID] => 1
[Title] => Welcome
[Content] => This is the first article.
)
)
)
Accessing Content:
echo 'Content: ' . $final_res->articles[0]->Content;
Output:
Content: This is the first article.
Run code