Skip to content
Advertisement

Cant access Json Array values created by json_decode

My json_decode() is returning an array but i just cant seem to access it. Here’s as much of information i think would be useful. I’m pulling a json value from a server using curl commands.

The response (output) from the server :

//php code from getVal.php 
$response = curl_exec($ch); 
echo($response);
var_dump($response);

enter image description here

{ "automat": false, "brightness": null, "increase: set time": 2, "decrease: set time": 2 } string(101) "{ "automat": false, "brightness": null, "increase: set time": 2, "decrease: set time": 2 } "     
 

Converting the json formatted string to json array:

//php code from getVal.php 
$res = json_decode($response, true);
echo($res);
echo("<br><br>");
var_dump($res);
echo("<br><br>");
echo($res["automat"]);

output: enter image description here

Array

array(4) { ["automat"]=> bool(false) ["brightness"]=> NULL ["increase: set time"]=> int(2) ["decrease: set time"]=> int(2) }

Clearly $res prints as Array for some reason, and when trying to print $res[“automat”] it doesn’t t print anything at all. I’ve checked the console and there is no errors.

Solutions already tried with no result :

//1: 
$response =  stripslashes($response); 
//2: 
$response = html_entity_decode($response);
//3: 
$nbsp = html_entity_decode( "&nbsp;" );
$response = str_replace( $nbsp, "", $response);
//for each above 
$res = json_decode($response, true); 

Any further information can be provided if asked.

Advertisement

Answer

You are trying to echo a bool with a false value. It will not return/print anything.

You can test this, be executing the following code:

echo '('. NULL .')' . "r";
echo '('. false .')'. "r";
echo '('. true .')' . "r";

This will give your output:

()
()
(1)

In order to see something from your example, use another key for this:

echo $res["increase: set time"];

P.S. Don’t use images for showing source code.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement