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 爬知乎登录之后内容加载不出来
  • ¥15 怎么用protues测量通频带
  • ¥15 zepelin使用sparkInterpreter 异常
  • ¥15 paho mqtt 接收不到消息
  • ¥15 函数r关于两个分量y,z方向上的图像,分开画r随y的图像,r随z的图像
  • ¥15 如何用Matlab求Pearcey函数的数值积分?
  • ¥15 一个简单的函数定义问题,输出结果是相反的,怎么回事呢?
  • ¥15 页面加载报错,不知道怎么处理
  • ¥20 vue-draggable-resizable选中状态保持
  • ¥15 flink cdc读取SqlServer2016