为了账号安全,请及时绑定邮箱和手机立即绑定

我需要从 JSON 中获取密钥字符串

我需要从 JSON 中获取密钥字符串

PHP
一只斗牛犬 2023-09-08 14:30:16
我在 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它则会将对象转换为对象


查看完整回答
反对 回复 2023-09-08
?
幕布斯7119047

TA贡献1794条经验 获得超8个赞

  • 将 json 转换为数组:

$array = json_decode($json, true);

  • 要获取关联数组中的第一个键:

$firstKey = array_key_first($array['问题']);

$firstKey将包含您的动态密钥。


查看完整回答
反对 回复 2023-09-08
  • 2 回答
  • 0 关注
  • 63 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信