Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when get value from json_decode() in php?

I have a sample code:

$description = '{"2G Network":"GSM 850 / 900 / 1800 / 1900 ","3G Network":"HSDPA 850 / 900 / 1700 / 1900 / 2100 "}';
$data = json_decode($description);
echo $data->2G Network;

 // OR echo $data['2G Network'];

result is error, how to fix it !

like image 293
Hai Truong IT Avatar asked Dec 06 '25 18:12

Hai Truong IT


1 Answers

Try this:

echo $data->{'2G Network'};

The problem wasn't with JSON, but that you had a space in the object property you were trying to access. If you use curly braces { }, then you can use strings to name the property you want to get/set.

like image 173
Brad Avatar answered Dec 08 '25 14:12

Brad