Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.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
Php 从嵌套json响应获取值_Php_Json - Fatal编程技术网

Php 从嵌套json响应获取值

Php 从嵌套json响应获取值,php,json,Php,Json,这是我的json响应,我想在这里获取用户Id值 我看到了类似的线索,但没有帮助我 以下是我如何实现的代码: $tweets3 = $connection->get("https://api.twitter.com/1.1/statuses/home_timeline.json?include_entities=true&trim_user=".$userid.""); foreach ($tweets3 as $item) {

这是我的json响应,我想在这里获取用户Id值

我看到了类似的线索,但没有帮助我

以下是我如何实现的代码:

$tweets3 = $connection->get("https://api.twitter.com/1.1/statuses/home_timeline.json?include_entities=true&trim_user=".$userid."");
        foreach ($tweets3 as $item)
        {
                 echo $item->text;
                echo "<br/>";
                $tweet = $item->text;
                $userid = $item->user; // How to get user id here?
        }
$tweets3=$connection->get(“https://api.twitter.com/1.1/statuses/home_timeline.json?include_entities=true&trim_user=“$userid.”;
foreach($tweets3作为$item)
{
echo$item->text;
回声“
”; $tweet=$item->text; $userid=$item->user;//如何在此处获取用户id? }
和Json响应:

[
  {
    "coordinates": null,
    "truncated": false,
    "created_at": "Tue Aug 28 21:16:23 +0000 2012",
    "favorited": false,
    "id_str": "240558470661799936",
    "in_reply_to_user_id_str": null,
    "entities": {
      "urls": [
      ],
      "hashtags": [
      ],
      "user_mentions": [
      ]
    },
    "text": "just another test",
    "contributors": null,
    "id": 240558470661799936,
    "retweet_count": 0,
    "in_reply_to_status_id_str": null,
    "geo": null,
    "retweeted": false,
    "in_reply_to_user_id": null,
    "place": null,
    "source": "<a href=\"http://realitytechnicians.com\" rel=\"nofollow\">OAuth Dancer Reborn</a>",
    "user": {
      "name": "OAuth Dancer",
      "profile_sidebar_fill_color": "DDEEF6",
      "profile_background_tile": true,
      "profile_sidebar_border_color": "C0DEED",
      "profile_image_url": "http://a0.twimg.com/profile_images/730275945/oauth-dancer_normal.jpg",
      "created_at": "Wed Mar 03 19:37:35 +0000 2010",
      "location": "San Francisco, CA",
      "follow_request_sent": false,
      "id_str": "119476949",
      "is_translator": false,
      "profile_link_color": "0084B4",
      "entities": {
        "url": {
          "urls": [
            {
              "expanded_url": null,
              "url": "http://bit.ly/oauth-dancer",
              "indices": [
                0,
                26
              ],
              "display_url": null
            }
          ]
        },
        "description": null
      },
      "default_profile": false,
      "url": "http://bit.ly/oauth-dancer",
      "contributors_enabled": false,
      "favourites_count": 7,
      "utc_offset": null,
      "profile_image_url_https": "https://si0.twimg.com/profile_images/730275945/oauth-dancer_normal.jpg",
      "id": 119476949,
      "listed_count": 1,
      "profile_use_background_image": true,
      "profile_text_color": "333333",
      "followers_count": 28,
      "lang": "en",
      "protected": false,
      "geo_enabled": true,
      "notifications": false,
      "description": "",
      "profile_background_color": "C0DEED",
      "verified": false,
      "time_zone": null,
      "profile_background_image_url_https": "https://si0.twimg.com/profile_background_images/80151733/oauth-dance.png",
      "statuses_count": 166,
      "profile_background_image_url": "http://a0.twimg.com/profile_background_images/80151733/oauth-dance.png",
      "default_profile_image": false,
      "friends_count": 14,
      "following": false,
      "show_all_inline_media": false,
      "screen_name": "oauth_dancer"
    },
    "in_reply_to_screen_name": null,
    "in_reply_to_status_id": null
  },
  {
[
{
“坐标”:空,
“截断”:false,
“创建时间”:“2012年8月28日星期二21:16:23+0000”,
“偏爱”:错误,
“id_街”:“240558470661799936”,
“在对用户id的回复中”:null,
“实体”:{
“URL”:[
],
“hashtags”:[
],
“用户_提到”:[
]
},
“文本”:“只是另一个测试”,
“贡献者”:空,
“id”:240558470661799936,
“转发计数”:0,
“在对状态的回复中”id“str”:空,
“geo”:空,
“转发”:错误,
“in_reply_to_user_id”:null,
“地点”:空,
“来源”:“,
“用户”:{
“名字”:“OAuth舞者”,
“配置文件\侧边栏\填充\颜色”:“DDEEF6”,
“配置文件\u背景\u平铺”:正确,
“配置文件\侧边栏\边框\颜色”:“C0”,
“配置文件\图像\ url”:http://a0.twimg.com/profile_images/730275945/oauth-dancer_normal.jpg",
“创建时间”:“Wed Mar 03 19:37:35+0000 2010”,
“地点”:“加利福尼亚州旧金山”,
“跟踪请求发送”:false,
“id_街”:“119476949”,
“is_translator”:错误,
“配置文件链接颜色”:“0084B4”,
“实体”:{
“url”:{
“URL”:[
{
“扩展url”:空,
“url”:”http://bit.ly/oauth-dancer",
“指数”:[
0,
26
],
“显示url”:空
}
]
},
“说明”:空
},
“默认配置文件”:false,
“url”:”http://bit.ly/oauth-dancer",
“已启用贡献者”:false,
“最受欢迎项”统计:7,
“utc_偏移量”:空,
“配置文件\u图像\u url\u https”:https://si0.twimg.com/profile_images/730275945/oauth-dancer_normal.jpg",
“id”:119476949,
“列出的计数”:1,
“profile\u use\u background\u image”:真,
“配置文件\文本\颜色”:“333333”,
“粉丝数”:28,
“郎”:“恩”,
“受保护”:错误,
“geo_enabled”:正确,
“通知”:错误,
“说明”:“,
“配置文件\背景\颜色”:“C0”,
“已验证”:错误,
“时区”:空,
“配置文件\u背景\u图像\u url\u https”:https://si0.twimg.com/profile_background_images/80151733/oauth-dance.png",
“状态计数”:166,
“配置文件\背景\图像\ url”:http://a0.twimg.com/profile_background_images/80151733/oauth-dance.png",
“默认_配置文件_图像”:false,
“朋友数”:14,
"跟随":假,,
“显示所有内联媒体”:false,
“屏幕名称”:“oauth_舞者”
},
“回复至屏幕名称”为空,
“在对状态id的回复中”:null
},
{
更新代码

$tweets3 = $connection->get("https://api.twitter.com/1.1/statuses/home_timeline.json?include_entities=true&trim_user=".$userid."");
        $data = json_decode($tweets3,true);
         print_r($data);
        foreach($data as $key=>$val){
                 echo "--------------------------------<br />";
                 echo "Tweet : ".$val["text"];
                 echo "<br />";
                 echo "User : ".$val["user"]["name"];
                 echo "<br />--------------------------------<br />";
        }
$tweets3=$connection->get(“https://api.twitter.com/1.1/statuses/home_timeline.json?include_entities=true&trim_user=“$userid.”;
$data=json_decode($tweets3,true);
打印(数据);
foreach($key=>$val的数据){
回声“-------------------------------------
”; echo“Tweet:.$val[“text”]; 回声“
”; echo“User:”.$val[“User”][“name”]; 回声“
-------------------------------------
”; }
但这不会给出任何输出。

您可以这样做

$tweets3 = $connection->get("https://api.twitter.com/1.1/statuses/home_timeline.json?include_entities=true&trim_user=".$userid."");

$data = json_decode($tweets3,true);
现在,
$data
将成为一个数组,您可以循环并获取值

显示项目的更新

foreach($data as $key=>$val){
 echo "--------------------------------<br />";
 echo "Tweet : ".$val["text"];
 echo "<br />";
 echo "User : ".$val["user"]["name"];
 echo "<br />--------------------------------<br />";

}
foreach($key=>$val的数据){
回声“-------------------------------------
”; echo“Tweet:.$val[“text”]; 回声“
”; echo“User:”.$val[“User”][“name”]; 回声“
-------------------------------------
”; }

在上面我刚刚显示了tweet文本和用户名,你可以
在循环之前打印($data)
,查看不同的元素名称,并可以在循环中显示。

你可以添加详细代码吗?我不知道如何在此处循环用户id,你可以回显$tweets3,看看这是否返回JSON。让我们看看