解码JSON嵌套PHP(Laravel)



对不起,如果这是新秀标准。筑巢确实使我感到困惑。代码在这里:

$json= '[{ "all":
 "{"data":
 [ {"name": "Kofi", "age":13}, {"name": "Jay", "age":17} ]
}"
}]' ;
$decode = json_decode($json);
$names= $decode->all->data->name;
// I want to retrieve "Kofi" and "Jay"
foreach ($names as $name){
echo $name;
}

我想检索 科菲,杰伊我得到错误: 试图获得属性的"全部"非对象

我运行了您的json,它的格式不正确。我已经格式化了,这额外的代码应该为您完成工作。

注意:唯一的区别是"all": "{"...."}"更改为"all": { .... }

    $json= '[
            {
                "all":
                    {
                        "data":
                                [ 
                                    { "name": "Kofi", "age":13}, 
                                    {"name": "Jay", "age":17} 
                                ]
                    }
            }
        ]';
    $decode = json_decode($json);
    foreach($decode[0]->all->data as $dec) {
        echo $dec->name. '<br/>';
    }

最新更新