我在 Laravel API 請求中包含以下 JSON:{ "questionary": { "directLeader": { "answer": "ALWAYS", "comments": "asdf" } }, "id": 14 }我需要獲取字符串“directLeader”,因為該鍵在請求中發生變化,我將其用作查詢更新的參考。
2 回答

慕標5832272
TA貢獻1966條經驗 獲得超4個贊
你需要json_decode()像這樣的 json
$json = '{
"questionary": {
"directLeader": {
"answer": "ALWAYS",
"comments": "asdf"
}
},
"id": 14
}';
$encoded_json = json_decode($json, true);
dd($encoded_json['questionary']['directLeader']);
請注意,true in 會將json_decode()對象轉換為數組,沒有true它則會將對象轉換為對象

幕布斯7119047
TA貢獻1794條經驗 獲得超8個贊
將 json 轉換為數組:
$array = json_decode($json, true);
要獲取關聯數組中的第一個鍵:
$firstKey = array_key_first($array['問題']);
$firstKey將包含您的動態密鑰。
- 2 回答
- 0 關注
- 154 瀏覽
添加回答
舉報
0/150
提交
取消