【问题标题】:Printing a specific value in array PHP在数组 PHP 中打印特定值
【发布时间】:2015-05-20 13:37:19
【问题描述】:

我只想输出一次followers_count 的值。我使用foreach 循环设法做到了这一点,但这显然会多次输出结果。还有其他一些简单的方法可以做到这一点吗?

数组声明如下:

$tweetsshow = $connection->get("https://api.twitter.com/1.1/users/show.json?screen_name=".$twitteruser);
echo $tweetsshow[0]->user->followers_count;



array(30) {
  [0]=>
  object(stdClass)#5 (24) {
    ["created_at"]=>
    string(30) "Wed May 20 12:32:30 +0000 2015"
    ["id"]=>
    float(6.010025574446E+17)
    ["id_str"]=>
    string(18) "601002557444595712"
    ["text"]=>
    string(79) "Judge Farrugia Sacco loses appeal http://t.co/njcgvTDTej http://t.co/GRjnBt13uC"
    ["source"]=>
    string(71) "Times of Malta"
    ["truncated"]=>
    bool(false)
    ["in_reply_to_status_id"]=>
    NULL
    ["in_reply_to_status_id_str"]=>
    NULL
    ["in_reply_to_user_id"]=>
    NULL
    ["in_reply_to_user_id_str"]=>
    NULL
    ["in_reply_to_screen_name"]=>
    NULL
    ["user"]=>
    object(stdClass)#6 (39) {
      ["id"]=>
      int(390687940)
      ["id_str"]=>
      string(9) "390687940"
      ["name"]=>
      string(14) "Times of Malta"
      ["screen_name"]=>
      string(15) "TheTimesofMalta"
      ["location"]=>
      string(5) "Malta"
      ["description"]=>
      string(74) "General, sporting, and business news for Malta and the surrounding region."
      ["url"]=>
      string(22) "http://t.co/OYjxN0Y4tX"
        ["description"]=>
        object(stdClass)#10 (1) {
          ["urls"]=>
          array(0) {
          }
        }
      }
      ["protected"]=>
      bool(false)
      ["followers_count"]=>
      int(13865)
      ["friends_count"]=>
      int(13)
      ["listed_count"]=>
      int(153)
      ["created_at"]=>
      string(30) "Fri Oct 14 11:20:15 +0000 2011"
      ["favourites_count"]=>
      int(0)

【问题讨论】:

    标签: php arrays json object


    【解决方案1】:

    你可以这样做:

    echo $array[0]->user->followers_count; //Returns 13865
    

    【讨论】:

    • 我最初尝试过,但收到此错误“无法使用 stdClass 类型的对象作为数组”
    • 我怀疑您能否在尝试回显数组的地方发布更多代码?
    • 您的代码有效,错误地使用了不正确的数组。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多