0

I have to delete some objects inside my action before sending data to the view

The data are something like this:

'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',


            }
        ],
    }
]

favorites is an array i want to remove some specific object inside this array according to a condition.. So that what i tried to do :

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

Butit doesn't work can some one help ?

1
  • Collect the elements that you dont want to remove into a new array. At the moment you are overwriting the original list Commented Apr 18, 2016 at 11:52

1 Answer 1

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

You have to reference the object directly.

Sign up to request clarification or add additional context in comments.

2 Comments

Than you for helping have i did a mistake in the post why i get downvote ?
People downvote on here just for fun sometimes. IDK why, it seemed like a reasonable question to me. Gave you a +1 though. Thanks for the check.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.