【问题标题】:PHP - Converting object array to usable objectPHP - 将对象数组转换为可用对象
【发布时间】:2011-06-24 17:49:14
【问题描述】:

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

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

感谢您的帮助

安迪

例如

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

        )
)

【问题讨论】:

  • 这不是将数组转换为对象,是吗?
  • :) 刚刚意识到我可以只做 $myObjArray[0]->methodCall(),所以我将把它包装在一个遍历每个元素的 for 循环中
  • 是的,你只有一个对象数组。

标签: php arrays object multidimensional-array


【解决方案1】:

您可以使用 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
  }
}
?>

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

【讨论】:

  • 我终于想通了,但您的回复仍然值得 +1 回答,感谢您的帖子
猜你喜欢
  • 2022-01-20
  • 2019-09-18
  • 1970-01-01
  • 2022-01-14
  • 2020-06-15
  • 2016-03-11
  • 2015-12-10
  • 2019-07-11
相关资源
最近更新 更多