Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从json检索属性?PHP_Php_Json - Fatal编程技术网

从json检索属性?PHP

从json检索属性?PHP,php,json,Php,Json,我有一个部分json: string(1583) "[{"user_id":"U4qtgydvSgG9zV9F"}] 如何获取用户id值 我试过这个: $json = json_decode($res); $UserId = $res->user_id; 但是我没有点击用户id 然后,我尝试将结果强制转换为一个数组,以访问用户id,如下所示: $res = (array)file_get_contents($url, false, $context); echo $res['user_

我有一个部分json:

string(1583) "[{"user_id":"U4qtgydvSgG9zV9F"}]
如何获取用户id值

我试过这个:

$json = json_decode($res);
$UserId = $res->user_id;
但是我没有点击用户id

然后,我尝试将结果强制转换为一个数组,以访问用户id,如下所示:

$res = (array)file_get_contents($url, false, $context);
echo $res['user_id'];
array(1) { [0]=> string(1583) "[{"user_id":"U4qtgydvSgG9zV9F-NB2rQ"
该数组如下所示:

$res = (array)file_get_contents($url, false, $context);
echo $res['user_id'];
array(1) { [0]=> string(1583) "[{"user_id":"U4qtgydvSgG9zV9F-NB2rQ"
如何获取用户id值?谢谢

请参见此json示例:

$j = '[{"user_id":"U4qtgydvSgG9zV9F"}]';
$j = json_decode($j);
echo $j[0]->user_id;

试试这个:
$UserId=$res[0]->user\u id您发布的JSON字符串无效,如果没有其他字符串,则无法查看结构。首先需要去掉“string(1583)”部分,因为这不是JSON的一部分。json字符串来自何处,在我看来,它像是var_dump()的结果。