gpt4 book ai didi

json - 如何在 Laravel 中获取 Json 数据

转载 作者:行者123 更新时间:2023-12-01 13:40:54 25 4
gpt4 key购买 nike

我有来自角度前端的 JSON 响应。响应是:

 {
"selected": null,
"lists": [
{
"board one": [
{
"card_name": "Card 10",
"id": 1,
"card_unique_id": "PC1"
},
{
"card_name": "card 11",
"id": 2,
"card_unique_id": "PC2"
},
{
"card_name": "card12",
"id": 3,
"card_unique_id": "PC3"
},
{
"card_name": "adfaf",
"id": 12,
"card_unique_id": "PC12"
},
{
"card_name": "fa df af ad",
"id": 13,
"card_unique_id": "PC13"
}
],
"BoardId": "PB1"
},
{
"board two": [
{
"card_name": "Card 20",
"id": 4,
"card_unique_id": "PC4"
},
{
"card_name": "CArd 21",
"id": 5,
"card_unique_id": "PC5"
},
{
"card_name": "card 23",
"id": 6,
"card_unique_id": "PC6"
},
{
"card_name": "Card 24",
"id": 7,
"card_unique_id": "PC7"
}
],
"BoardId": "PB2"
},
{
"board three": [
{
"card_name": "Card 30",
"id": 8,
"card_unique_id": "PC8"
},
{
"card_name": "card 31",
"id": 9,
"card_unique_id": "PC9"
},
{
"card_name": "card 32",
"id": 10,
"card_unique_id": "PC10"
}
],
"BoardId": "PB3"
},
{
"new board": [
{
"card_name": "card 40",
"id": 11,
"card_unique_id": "PC11"
}
],
"BoardId": "PB4"
}
]
}

我想在 laravel 中获取所有卡片名称。在 laravel Controller 函数中,我这样编写代码:它返回一些错误。我不知道如何获取所有这些 card_names

    public function sortupdate(Request $response)
{
$get_result_arr = json_decode($response->getContent());

foreach($get_result_arr as $result){
$lists = $result->lists;
}
}

请大家帮忙。感谢高级..

最佳答案

json_decode()中加入true得到一个关联数组:

$get_result_arr = json_decode($response->getContent(), true);

然后遍历这个数组并得到每个card_name:

foreach ($get_result_arr['lists'] as $list) {
foreach (array_values($list)[0] as $card) {
echo $card['card_name'];
}
}

关于json - 如何在 Laravel 中获取 Json 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40380272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com