2011-02-10 3 views
1

로 변환 객체 배열 나는,이 상태 개체는 상태 객체를 포함하는 배열과 같은 객체의 배열을 포함하고 또한 댓글이 내 질문에 지금은 내 배열의 객체를 가지고있다PHP - 사용 가능한 객체

객체를 포함했다 어떻게 다시 꺼내야합니까? 이것은 나중에 DB에 저장할 수 있습니다. 당신의 도움에 대한

감사

앤디

예를 들어,

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

     ) 
) 
+1

배열을 객체로 변환하지 않습니다. 그렇습니까? – BoltClock

+1

:) 그냥 $ myObjArray [0] -> methodCall()을 수행 할 수 있다는 것을 알았으므로 for 루프에서 각 요소를 반복하여 포장합니다. – Garbit

+0

그래, 방금 객체 배열이 있습니다. –

답변

2

개별 개체의 foreach 및 access 속성을 사용하여 저장할 수 있습니다. 모든 속성이 private이기 때문에 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 
    } 
} 
?> 

이 당신 같은 복잡한 중첩 된 객체에 특히 유용합니다.

+0

궁극적으로 알아 냈지만 답장을 보내려면 +1 답변을 받아야합니다. 감사합니다. – Garbit