dongzhenbi8919 2019-07-26 14:58
浏览 143
已采纳

如何在php中解码json中格式化的数组?

I am setting up dialogflow in php to retrieve input from google assistant.

I want to use a parameter I got from dialogflow.

But I don't know how to get it out of the json.

I've already got a function that gets parameters from the json, but it's in a weird format.

How can I get only the pet_name in a variable?

The formats are:

When I use print_r I get:

Array
(
    [pet_name] => gizmo
)

And when I do var_dump I get:

array(1) {
  ["pet_name"]=>
  string(5) "gizmo"
}

Hope this makes it clearer

  • 写回答

3条回答 默认 最新

  • doushan3511 2019-07-26 15:16
    关注

    You can get the name from your index as like:

    <?
    $string = '{"pet_name":"gizmo"}';
    $parsed = json_decode($string);
    echo $parsed->pet_name;
    ?>
    

    if you are using true as a second param in json_decode() then this will return an array and you can get as like:

    <?
    $string = '{"pet_name":"gizmo"}';
    $parsed = json_decode($string,true);
    echo $parsed['pet_name'];
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 征集Python提取PDF文字属性的代码
  • ¥15 如何联系真正的开发者而非公司
  • ¥15 有偿求苍穹外卖环境配置
  • ¥15 代码在keil5里变成了这样怎么办啊,文件图像也变了,
  • ¥20 Ue4.26打包win64bit报错,如何解决?(语言-c++)
  • ¥15 clousx6整点报时指令怎么写
  • ¥30 远程帮我安装软件及库文件
  • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
  • ¥20 (求远程解决)深信服vpn-2050这台设备如何配置才能成功联网?
  • ¥15 Arduino的wifi连接,如何关闭低功耗模式?