I am trying to decode a JSON string into an array but i get the following error.
Đang xem: Php Json_Decode() Function
Fatal error: Cannot use object of type stdClass as array in C:wampwww empasklaila.php on line 6
Here is the code:
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 “”;print_r($obj);
This is a late contribution, but there is a valid case for casting json_decode with (array).Consider the following:
$jsondata = “”;$arr = json_decode($jsondata, true);foreach ($arr as $k=>$v){ echo $v; // etc.}If $jsondata is ever returned as an empty string (as in my experience it often is), json_decode will return NULL, resulting in the error Warning: Invalid argument supplied for foreach() on line 3. You could add a line of if/then code or a ternary operator, but IMO it”s cleaner to simply change line 2 to …
$arr = (array) json_decode($jsondata,true);… unless you are json_decodeing millions of large arrays at once, in which case as
Just in case you are working on php less then 5.2 you can use this resourse.
http://mike.teczno.com/JSON/JSON.phps
According to the PHP Documentation json_decode function has a parameter named assoc which convert the returned objects into associative arrays
mixed json_decode ( string $json <, bool $assoc = FALSE > )Since assoc parameter is FALSE by default, You have to set this value to TRUE in order to retrieve an array.
Examine the below code for an example implication:
$json = “{“a”:1,”b”:2,”c”:3,”d”:4,”e”:5}”;var_dump(json_decode($json));var_dump(json_decode($json, true));which outputs:
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)}
json_decode support second argument, when it set to TRUE it will return an Array instead of stdClass Object. Check the Manual page of json_decode function to see all the supported arguments and its details.
For example try this:
$json_string = “http://www.example.com/jsondata.json”;$jsondata = file_get_contents($json_string);$obj = json_decode($jsondata, TRUE); // Set second argument as TRUEprint_r($obj<"Result">); // Now this will works!
json_decode($data, true); // Returns data in array format json_decode($data); // Returns collections So, If want an array than you can pass the second argument as “true” in json_decode function.
I hope this will help you
$json_ps = “{“courseList”:< {"course":"1", "course_data1":"Computer Systems(Networks)"}, {"course":"2", "course_data2":"Audio and Music Technology"}, {"course":"3", "course_data3":"MBA Digital Marketing"} >}”;Use Json decode function
$json_pss = json_decode($json_ps, true);Looping over JSON array in php
foreach($json_pss<"courseList"> as $pss_json){ echo “” .$course_data1 = $pss_json<"course_data1">; exit; }Result: Computer Systems(Networks)
in PHP json_decode convert json data into PHP associated arrayFor Ex: $php-array= json_decode($json-data, true);print_r($php-array);
Try like this:
$json_string = “https://example.com/jsondata.json”;$jsondata = file_get_contents($json_string);$obj = json_decode($jsondata);print_r($obj->Result);foreach($obj->Result as $value){ echo $value->id; //change accordingly}
Highly active question. Earn 10 reputation in order to answer this question. The reputation requirement helps protect this question from spam and non-answer activity.
Not the answer you're looking for? Browse other questions tagged php arrays json or ask your own question.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev2021.3.22.38863