我要刪除我的行動裏面的一些對象將數據發送到視圖之前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不起作用有人能幫忙嗎?
將您不想刪除的元素收集到新數組中。目前你正在覆蓋原始列表 –