1

I have 50 items in an array that looks something like this:

     array:50 [▼
      0 => {#253 ▼
        +"message": "message_1"
        +"created_time": {#254 ▼
          +"date": "2016-03-03 07:54:05.000000"
          +"timezone_type": 1
          +"timezone": "+00:00"
        }
        +"id": "167633226631991_1051771021551536"
      }
      1 => {#255 ▼
        +"message": "message_2"
        +"created_time": {#256 ▼
          +"date": "2016-03-02 13:35:26.000000"
          +"timezone_type": 1
          +"timezone": "+00:00"
        }
        +"id": "167633226631991_1051313571597281"
      }
    ]

I need to grab each variable within the array. But I keep getting an error that reads:

Undefined property: stdClass::$message

Im not sure what Im missing I've tried doing:

     foreach ($posts as $post) {
            $test = array('message' => $post->message );
     }

and also

$a = -1;

$alpha = 0;

$omega = count($posts);

$empty_array = array();

foreach (range($alpha,$omega) as $i) {
  ++$a;
  $test = $posts[$a]->message;
  array_push($empty_array, $test);
  }

but I'm getting the same error. Im using laravel 5.2.

Update: The array is coming from facebook's graph API. I am converting the json format using:

$posts = json_decode($userNode['posts']);

Doing this then displays the array:50 as above.

6
  • Try to write $post['message'] Commented Mar 6, 2016 at 8:13
  • What does dd($post) show within the foreach? Commented Mar 6, 2016 at 8:29
  • @RaviHirani I tried $post['message'] I get this error Cannot use object of type stdClass as array Commented Mar 6, 2016 at 9:06
  • @Joseph if I dd($post) I get {#253 ▼ +"message": "message_1" +"created_time": {#254 ▼ +"date": "2016-03-03 07:54:05.000000" +"timezone_type": 1 +"timezone": "+00:00" } +"id": "167633226631991_1051771021551536" } Commented Mar 6, 2016 at 9:07
  • Where is this array coming from in the first place, and what is the code used to generate it? Please update this information in the question rather than as a comment as it's easier to read. Commented Mar 6, 2016 at 9:19

1 Answer 1

2

As the array is an array of objects, you are referencing it correctly when you type $post->message. Doing $post['message'] won't work because it is not an array of arrays.

I believe that the Undefined property: stdClass::$message error is coming about because some of the objects in your $posts array have an empty +message or a non-existent +message

The best way to deal with this is to use an isset(). However, you also need to change your $test to $test[] to stop it from being overwritten each time it loops.

The code:

    foreach ($posts as $post) {
        isset($post->message) ? $test[] = array('message' => $post->message ) : null;
    }

After that, dd($test) will provide you with an array of arrays with the message in.

If you actually just wanted a plain array of messages then do this:

    foreach ($posts as $post) {
        isset($post->message) ? $test[] = $post->message : null;
    }
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks a lot! Worked like a charm.
@Joseph: Nice answer brother :) Perhaps in laravel object can be accessible as array also. stackoverflow.com/questions/22897919/…
You can also just use the Laravel helper array_pluck($posts, 'message') :)
@RaviHirani You're right, in some cases they are interchangeable. However, in this specific case, accessing $post['message'] would result in Cannot use object of type stdClass as array
Agree with you my friend. This is not the case :-) BTW my name is Ravi not others as you have mentioned in your answer ;-)
|

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.