0

My variable looks like this:

var_dump($content)

object(stdClass)#5 (1) {
  ["errors"]=>
  array(1) {
    [0]=>
    object(stdClass)#6 (2) {
      ["message"]=>
      string(24) "Invalid or expired token"
      ["code"]=>
      int(89)
    }
  }
}

How can a get the value of message ("Invalid or expired token")?

1
  • OK, I've edited your question into shape and added a hopefully canonical answer. This question pops up again and again every day. Hope this will be the canonical entry which we can close other duplicate questions against. Please don't feel that it's welcome to ask such basic questions, you should really figure out such simple things using the manual or Google. However, since it is a common problem that people do not do so, let's let this one stand here for all to see. Commented Jul 7, 2014 at 13:27

2 Answers 2

1

If a value says object(stdClass) in a var_dump, you access its properties with the -> notation. So the first step in your case is:

$content->errors

This now is an array, so you access its keys using [..] notation. The next step down is:

$content->errors[0]

This now again is a object(stdClass), so you access its properties using ->:

$content->errors[0]->message

However, this structure implies that there may be multiple errors. You should rather loop through the errors array and output all errors in turn:

foreach ($content->errors as $error) {
    echo $error->message;
}
Sign up to request clarification or add additional context in comments.

3 Comments

You might want to buld a string in the foreach instead of just echoing, that will give you the ability to work with the string more.
@Brett You may want to take a number of actions based on the actual errors in that array, not just echo them.
Agreed, that structure also gives @user3128221 access to the error code as well.
0

You would have to iterate and you can access the message var directly:

<?php

foreach ($content->errors as $item)
{
   echo $item->message;
}

1 Comment

was in a hurry, my bad

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.