PHP - 将对象数组转换为可用对象

发布于 2024-10-16 22:06:51 字数 2721 浏览 1 评论 0原文

我有一个包含状态对象的数组,这些状态对象包含一个类似对象的数组,还包含注释对象

我的问题是,现在我的数组中有这些对象,如何将它们拉出来?这样我以后就可以将它们保存到数据库中。

感谢您的帮助

安迪,

例如

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
                (
                )

        )
)

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
                (
                )

        )
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸 2024-10-23 22:06:51

您可以使用 foreach 并访问要保存的各个对象的属性。我假设您正在使用 getter 和 setter 方法,因为您的所有属性都是私有的。使用 foreach 提供“as”关键字,以便在循环执行时为每个单独的对象实例创建别名。

<?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
  }
}
?>

这对于像您这样的复杂嵌套对象来说特别方便,因为公共方法和属性可以由单独的迭代器访问以进行简单的操作,例如保存到数据库。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文