duanbarong4321 2017-05-11 18:13
浏览 362

PHP:从JSON数组中提取值

I have an API that takes parameters from a post through JSON and I want to extract one of the values in an array for a key value pair. However, despite many attempts, I can't get it to work.

Here is my code:

$json = file_get_contents('php://input'); 
$request = json_decode($json, true);
$parameters = $request["result"]["parameters"];

When I log $parameters to a text file, it log as

 {"numberofhits":"5"}

However, my efforts to capture the value 5 are not working:

$numhits = $json['numberofhits']; logs as empty.

So does:

$numhits = $parameters->numberofhits;

How can I capture the value 5 in a variable?

  • 写回答

2条回答 默认 最新

  • dqzlqfqeh845799833 2017-05-11 18:20
    关注

    If your $parameters just shows {"numberofhits":"5"}, this means your post request just returned a json of a json. Try json decoding $parameters again like json_decode($parameters, true); and log that result. It should log the desired array.

    评论

报告相同问题?

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。