1

I am trying to access a property called 'ChatId' that is returned in the curl_exec command to the variable 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:

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.

2
  • 1
    $output["Chats"][0]["Chat"]["ChatId"] Commented Apr 24, 2020 at 10:10
  • Thanks for this it worked fine! I was having trouble with that Commented Apr 24, 2020 at 10:15

1 Answer 1

2

For future reference, each array(x) is opening up a new level of elements, where x is how many elements this level has. 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.

So

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

Hope this will help you in the future while working with arrays.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for your explanation, this will help me in future and I will use this approach when dealing with similar arrays in the future!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.