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条)

报告相同问题?

悬赏问题

  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'