I'm trying to print a value of my array but i keep having the same error "Warning: Illegal string offset".
I don't know why i keep having this error, it is a json array and it is supposed to be right ..
thank you in advance
$bson = MongoDB\BSON\fromPHP($entry);
$json= MongoDB\BSON\toJSON($bson);
var_dump($js);
echo $js['nom'];
string(1115) ""{ '_id' : { '$oid' : '5ca51d1b7d42c4ccb4d67618' }, 'nom' : 'Led Zeppelin', 'type' : 'G', 'pays' : 'Angleterre', 'genre' : 'Heavy Metal', 'albums' : [ { 'nom_album' : 'Led Zeppelin', 'annee' : '1969', 'prix' : '16.50', 'image' : '' }, { 'nom_album' : 'Led Zeppelin II', 'annee' : '1969', 'prix' : '16.50', 'image' : '' }, { 'nom_album' : 'Led Zeppelin III', 'annee' : '1970', 'prix' : '16.50', 'image' : '' }, { 'nom_album' : 'Led Zeppelin IV', 'annee' : '1971', 'prix' : '16.50', 'image' : '' }, { 'nom_album' : 'Houses Of The Holy', 'annee' : '1973', 'prix' : '13.20', 'image' : '' }, { 'nom_album' : 'Physical Graffiti', 'annee' : '1975', 'prix' : '16.50', 'image' : '' }, { 'nom_album' : 'Presence', 'annee' : '1976', 'prix' : '18.81', 'image' : '' }, { 'nom_album' : 'In Through The Outdoor', 'annee' : '1979', 'prix' : '18.70', 'image' : '' }, { 'nom_album' : 'Coda', 'annee' : '1982', 'prix' : '16.50', 'image' : '' }, { 'nom_album' : 'The Song Reamins The Same', 'annee' : '1976', 'prix' : '19.80', 'image' : '' }, { 'nom_album' : 'Live BBC Sessions', 'annee' : '1997', 'prix' : '19.86', 'image' : '' } ] }""
Warning: Illegal string offset 'nom' in C:\xampp\htdocs\music\index.php on line 23
i want to have : nom, nom_album in the same row
I already looked for other posts and i don't find my answer, i'm stuck on that
The $js is not array , that why you have the Warning
You have to decode json to array first:
$res = json_decode($js, true);
echo $res['nom'];