2

In php how would I get the value 'error' in this array below? I did a var_dump($myArray);

I tried echo $myArray[0][0]; and $myArray[0]; but neither of these worked.

array(1) {
      [0]=>
      array(1) {
        ["error"]=>
        array(4) {
          ["message"]=>
          string(27) "Invalid OAuth access token."
          ["type"]=>
          string(14) "OAuthException"
          ["code"]=>
          int(190)
          ["fbtrace_id"]=>
          string(11) "GJb4ZZLyAll"
        }
      }
    }

What I am actually looking for is to check the value of $myArray[0][0]; If my code works that value will be "id". If it didn't work it will be "error". So I need to see if it says "id" or "error".

3
  • 5
    echo $myArray[0]['error']['message']; Commented Dec 29, 2015 at 9:22
  • Try var_dump( array_column($myArray, ''error" )); and var_dump( array_column($myArray[0], ''error" )); Commented Dec 29, 2015 at 9:23
  • What I am actually looking for is to check the value of $myArray[0][0]; If my code works that value will be "id". If it didn't work it will be "error". So I need to see if it says "id" or "error". Commented Dec 29, 2015 at 9:29

2 Answers 2

2

As from my understanding you want to check the value of the key. So we get the keys as an array with array_keys(), with this you then can access the first key and check if it is id or error, e.g.

$keys = array_keys($myArray[0]);

if($keys[0] == "id") {
    //good
} elseif($keys[0] == "error") {
    //bad
}
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! Thank you Rizier123. Now I know why you have 43k points. :)
1

In PHP you can access array elements by name. So to get the value of "error" type:

$value = $myArray[0]["error"];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.