1

I have an array which contains status objects, these status objects contain an array of like objects and also contain comment objects

My question is that now I have the objects in my array, how do I pull them back out? This is so I can save them to a db later on.

Thanks for your help

Andy

e.g.

Array
(
    [0] => cStatus Object
        (
            [statusId:cStatus:private] => 123123123
            [message:cStatus:private] => powpowpow
            [updated_time:cStatus:private] => 2011-01-27T15:52:48+0000
            [likes:cStatus:private] => Array
                (
                )

            [comments:cStatus:private] => Comment Object
                (
                    [commentId:Comment:private] => 123123123
                    [created_time:Comment:private] => 2011-01-30T20:18:50+0000
                    [message:Comment:private] => Kazam
                    [name:Comment:private] => Blue man
                    [createdBy:Comment:private] => 124124
                    [likes:Comment:private] => Array
                        (
                        )

                )

        )

    [1] => cStatus Object
        (
            [statusId:cStatus:private] => 5125125
            [message:cStatus:private] => Gawdam fruit and fibre is tasty :D
            [updated_time:cStatus:private] => 2011-01-25T20:21:56+0000
            [likes:cStatus:private] => Array
                (
                    [0] => Like Object
                        (
                            [likeId:Like:private] => 120409086
                            [name:Like:private] => Jt
                        )

                )

            [comments:cStatus:private] => Array
                (
                )

        )

    [2] => cStatus Object
        (
            [statusId:cStatus:private] => 5215215
            [message:cStatus:private] => Dear 2
            [updated_time:cStatus:private] => 2011-01-18T08:28:50+0000
            [likes:cStatus:private] => Array
                (
                    [0] => Like Object
                        (
                            [likeId:Like:private] => 2456
                            [name:Like:private] => Edw2r
                        )

                    [1] => Like Object
                        (
                            [likeId:Like:private] => 2452412
                            [name:Like:private] => aw1
                        )

                    [2] => Like Object
                        (
                            [likeId:Like:private] => 12412411
                            [name:Like:private] => wqw
                        )

                )

            [comments:cStatus:private] => Array
                (
                )

        )
)
3
  • 1
    That's not converting an array to an object, is it? Commented Feb 10, 2011 at 9:59
  • 1
    :) Just realised I can just do $myObjArray[0]->methodCall(), So I'll just wrap it in a for loop iterating through each element Commented Feb 10, 2011 at 10:14
  • Yeah, you just have an array of objects. Commented Feb 10, 2011 at 10:35

1 Answer 1

2

You can use foreach and access properties of individual objects to be saved. I assume you are using getter and setter methods since all your properties are private. Using foreach provides the "as" keyword to make an alias for each individual object instance as the loop executes among them.

<?foreach($obj as $status){
  $status_text = $status->getMessage();
  //save this to database using your favored method;
  $comments = $status->getComments();
  //nest the foreach for all the comments to save them as well, if you like
  foreach($comments as $comment){
   //Save $comment here as well
  }
}
?>

This is especially handy for complex nested objects like yours, since public methods and properties can be accessed by the individual iterator for easy action, like saving to the database.

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

1 Comment

I figured it out eventually, but you still deserve the +1 answer for your response, thanks for your post

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.