Simone Simone - 3 months ago 22
PHP Question

How to PHP decode JSON from MYSQL

On MySQL I have this JSON value

{"length":1847,"data":[0,0,0,0,-46,37]}


On PHP I need to string in array the "data" objects, so I tried:

$json = $row['wave'];
$json_array = json_decode($json);
$json_wave = $json_array["data"];


It gives me
internal server error 500
, so I tried:

var_dump(json_decode($json));


What I got is:

object(stdClass)#7 (5) {
["length"]=>
int(1847)
["data"]=>
array(3694) {
[0]=>
int(0)
[1]=>
int(0)
[2]=>
int(0)
[3]=>
int(0)
[4]=>
int(-46)
[5]=>
int(37)
}
}


I need to output this string
0,0,0,0,-46,37
, why is my code not working?

Answer

So assuming you want that last variable to be the string you need this

$json_array = json_decode($json);
$json_wave = implode(',', $json_array->data);

Because your JSON contains an object, PHP puts it into an object by default. That's what var_dump means when it says stdClass

Comments