无法在 php 上打印数组或子数组

Can't print an array or subarray on php

我正在尝试打印我的数组的值,但我一直遇到同样的错误 "Warning: Illegal string offset".

我不知道为什么我一直有这个错误,它是一个 json 数组,它应该是正确的..

提前致谢


    $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

我想要: nom, nom_album 在同一行

我已经查找了其他帖子,但没有找到我的答案,我一直坚持下去

$js 不是数组,这就是为什么你有 警告

您必须先将 json 解码为数组:

$res = json_decode($js, true);
echo $res['nom'];