如何从laravel中的多维json数组中获取值

时间:2018-01-14 19:46:39

标签: php json laravel firebase laravel-5

我正在开发一个RESTfull API,它可以从firebase数据库中获取数据 我成功地检索了与此相同的数据

{
"Admin": {
    "-L-ck6q9elghYywXHneg": {
        "accepted": false,
        "date": "7/12/2017",
        "hour": "4:32",
        "numberOfPeople": "3",
        "tableInfo": ""
    }
},
"Guest": {
    "-KvJja_r0beLtdoRc29f": {
        "accepted": true,
        "date": "1/10/2017",
        "hour": "0:43",
        "numberOfPeople": "",
        "tableInfo": ""
    }
}

}

现在,我想访问"接受"两个记录中的字段,而不知道密钥是什么" Admin"或"访客"怎么可以这样做?

我试过这个

foreach ($users as $user) {
        $data[$counter]['accepted'] = $user->accepted;
        $counter++;
    }

但是没有成功

3 个答案:

答案 0 :(得分:0)

这是一个像这样的对象

foreach ($users as $key=>$user) { //changed this line
    $data[$counter]['accepted'] = $user->accepted;
    $counter++;
}

答案 1 :(得分:0)

尝试 $ user [0] - >接受

foreach ($users as $user) {
    $data[$counter]['accepted'] = $user[0]->accepted;
    $counter++;
}

答案 2 :(得分:0)

刚刚测试过,与您的数据完美配合:

$users = json_decode($users, true);
foreach ($users as $user) {
   $data[$counter]['accepted'] = array_first($user)['accepted'];
   $counter++;
}