mercredi 11 juillet 2018

Remove the second instance of a key value pair in a collection

So I am trying to determine if a app is part of a list of apps that are affected by downstream services. The following code is what I am currently trying to do:

  private function createAppArray($apps, $downApps){

        $final = collect([]);
        for($i = 0; $i < count($apps); $i++){
            foreach($downApps as $downApp){
                if($apps[$i]->id == $downApp->aid){
                    $test = collect($apps[$i]);
                    $final->push($test->merge(['status' => 'warning']));
                    $i++;
                } 
            }
        }


        if($final->count() < count($apps)){
            foreach($apps as $app){
                $test = collect($app);
                $final->push($test->merge(['status' => 'success']));
            }
            // dd($final);
        }


        return $final;
    }

As you can see from the screenshots below tough, it duplicates the app as both up and down due to me appending the content of $apps on the final collection that has the affected apps on it.

enter image description here

ALso attached is the contents of a dd($final):

enter image description here

Basically what I need to do is delete the second instance of a app that is repeated within this collection.

So if the id is the second occurrence then delete that collection, can anyone help me accomplish this I cant seem to figure it out?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire