2016-04-18 142 views
0

我要刪除我的行動裏面的一些對象將數據發送到視圖之前PHP刪除對象的內部數組

的數據是這樣的:

'items' => [ 
     (int) 0 => object(App\Model\Entity\Propriete) { 

     'id_propriete' => (int) 1, 
     // and other fields 
     'user' => object(App\Model\Entity\User) { 

      'id' => '1459436853', 
     //and other fields 
     }, 
     'favorites' => [ 
      (int) 0 => object(App\Model\Entity\Favorite) { 

       'id' => (int) 24, 
       'propriete_id' => (int) 1, 
       'user_id' => '1459438630', 
       'created' => object(Cake\I18n\FrozenDate) { 

        'time' => '2016-04-15T00:00:00+00:00', 
        'timezone' => 'UTC', 
        'fixedNowTime' => false 

       }, 
       'modified' => object(Cake\I18n\FrozenDate) { 

        'time' => '2016-04-15T00:00:00+00:00', 
        'timezone' => 'UTC', 
        'fixedNowTime' => false 

       }, 

      }, 
      (int) 1 => object(App\Model\Entity\Favorite) { 

       'id' => (int) 27, 
       'propriete_id' => (int) 1, 
       'user_id' => '1459436853', 
      , 
       'modified' => object(Cake\I18n\FrozenDate) { 

        'time' => '2016-04-18T00:00:00+00:00', 
        'timezone' => 'UTC', 
        'fixedNowTime' => false 

       }, 
       '[new]' => false, 
       '[accessible]' => [ 
        '*' => true 
       ], 
       '[dirty]' => [], 
       '[original]' => [], 
       '[virtual]' => [], 
       '[errors]' => [], 
       '[invalid]' => [], 
       '[repository]' => 'Favorites' 

      }, 
      (int) 2 => object(App\Model\Entity\Favorite) { 

       'id' => (int) 28, 
       'propriete_id' => (int) 1, 
       'user_id' => 'ae0dce23-584b-4907-b32e-1655d5e69e55', 


      } 
     ], 
    } 
] 

收藏是數組我想根據條件去除此數組內的某個特定對象。 所以我試圖做的事:

foreach($proprietes as $ad){ 
     foreach($ad->favorites as $favori) 
      if($favori->user_id !==$user['id']){ 
       unset($favori); 
      } 
    } 

Butit不起作用有人能幫忙嗎?

+0

將您不想刪除的元素收集到新數組中。目前你正在覆蓋原始列表 –

回答

1
foreach($proprietes as $ad){ 
     foreach($ad->favorites as $key=>$favori) 
      if($favori->user_id !==$user['id']){ 
       unset($ad->favorites[$key]); 
      } 
    } 

您必須直接引用對象。

+0

比你幫助我在文章中犯了一個錯誤,爲什麼我得到downvote? –

+1

有時候,人們只是爲了好玩而放棄了。 IDK爲什麼,這對我來說似乎是一個合理的問題。給你一個+1雖然。感謝您的支票。 –

+0

感謝您解決我的問題=) –

相關問題