简体   繁体   English

访问 PHP 数组中的元素

[英]Accessing elements from a PHP array

I am trying to access a property called 'ChatId' that is returned in the curl_exec command to the variable server_output.我正在尝试访问一个名为“ChatId”的属性,该属性在 curl_exec 命令中返回到变量 server_output。

$server_output = curl_exec($ch);
$output = json_decode($server_output, true);

After this I tried running:在此之后我尝试运行:

var_dump(get_object_vars($output));

But $output is actually an array and not an object. So when I run var_dump($output) I get the output:但是 $output 实际上是一个数组而不是 object。所以当我运行 var_dump($output) 时,我得到了 output:

array(1) {
  ["Chats"]=>
  array(1) {
    [0]=>
    array(1) {
      ["Chat"]=>
      array(3) {
        ["ChatId"]=>
        int(11845)
        ["UserId"]=>
        string(16) "d9729feb63a1a015"
        ["SystemId"]=>
        string(33) "d9729feb63a1a015~434534343"
      }
    }
  }
}

The only property I want to access is the ChatId property but I am having difficulty doing so.我想访问的唯一属性是 ChatId 属性,但我很难这样做。

For future reference, each array(x) is opening up a new level of elements, where x is how many elements this level has.为了将来参考,每个 array(x) 都打开了一个新的元素级别,其中 x 是该级别有多少元素。 So if we are to remove these and think of a structure similar to the directory structure we get this:因此,如果我们要删除这些并考虑类似于目录结构的结构,我们会得到:

$output
--->["Chats"]
    ---> [0]
         ---> ["Chat"]
             ---> ["ChatId"]
             ---> ["UserId"]
             ---> ["SystemId"]

So if ChatId was the file you would want to get to you would first have to go to the "Chats" directory, then the [0] one, then the ["Chat"] one and then you can select the file you want.因此,如果 ChatId 是您想要访问的文件,您首先必须 go 到“聊天”目录,然后是 [0] 目录,然后是 [“聊天”] 目录,然后您可以 select 找到您想要的文件。

So所以

 $output["Chats"][0]["Chat"]["UserId"]

Hope this will help you in the future while working with arrays.希望这对您将来使用 arrays 时有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM