PHP:从JSON数组中提取值 [英] PHP: Extract value from JSON array

查看:1032
本文介绍了PHP:从JSON数组中提取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个API,该API通过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.

这是我的代码:

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

当我将$parameters登录到文本文件时,其记录为

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

 {"numberofhits":"5"}

但是,我获取值5的努力没有用:

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

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

也是:

$numhits = $parameters->numberofhits;

如何在变量中捕获值5?

How can I capture the value 5 in a variable?

推荐答案

$json以上的代码中,是字符串而不是json对象,您应使用$request来访问点击数

From your code above $json is a string and not a json object, your should use $request to access numberofhits

$request['numberofhits']

这篇关于PHP:从JSON数组中提取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆