【问题标题】:how do I navigate and grab infos from an Array like this我如何从这样的数组中导航和获取信息
【发布时间】:2017-07-06 06:41:32
【问题描述】:

我正在尝试从该数组中导航和获取信息,但我没有取得多大成功,有什么提示吗?这是来自 Twitter API 的解码 JSON

我尝试了echo $array->statuses[0]->created_at;,但它不起作用,我在这里做错了吗?

Array
    (
        [statuses] => Array
            (
                [0] => stdClass Object
                    (
                        [created_at] => Thu Jul 06 04:05:06 +0000 2017
                        [id] => 882812623959584768
                        [id_str] => 882812623959584768
                        [text] => RT @blackdragonsBR: [R6] GG!! Fechamos o mapa Banco por 5x2 e conquistamos a liderança no #ALIENWARENOIGN #GOBD #Acer #Predator https://t.c…
                        [truncated] => 
                        [entities] => stdClass Object
                            (
                                [hashtags] => Array
                                    (
                                        [0] => stdClass Object
                                            (
                                                [text] => ALIENWARENOIGN
                                                [indices] => Array
                                                    (
                                                        [0] => 90
                                                        [1] => 105
                                                }

                                        )
                                }
                        }
                }
        }
}

【问题讨论】:

  • 好吧,我想我明白了,echo $array['statuses'][0]->created_at;
  • 只需在第一次访问时使用[],例如:$array[statuses][0]->created_at;

标签: php arrays stdclass


【解决方案1】:

您需要通过[] 而不是-> 访问数组。所以改变如下状态的访问权限

$array[statuses][0]->created_at;

【讨论】:

    【解决方案2】:

    您可以同时使用json_encodejson_decode,然后将变量进行数组化,这样您就可以轻松获取数据了。
    就这样

    $array=json_decode(json_encode($array,true),true);
    $array[statuses][0]['created_at'];
    

    它会为你工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-17
      • 2021-05-27
      • 1970-01-01
      • 2021-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多