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

46
July 12, 2018, at 02:20 AM

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.

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

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?

Answer 1

Rather than creating 2 separate loops and trying to merge the results, you can cut down on a ton of code just by looping over the entire list of applications ($apps) and checking to see if the ID exists in the $downApps collection.

private function createAppArray($apps, $downApps) {
    $final = collect([]);
    foreach($apps as $app){
        $test = collect($app);
        // check to see if the down apps collection as this app
        if (!$downApps->where('aid', $app->id)->isEmpty()){
            $final->push($test->merge(['status' => 'warning']));
        } else {
            $final->push($test->merge(['status' => 'success']));
        }
    }
    return $final;
}

This method makes the logic much easier to read.

READ ALSO
PHP replace web address test in variable

PHP replace web address test in variable

can anyone help with this? I have a web address as a variable and would like to replace multiple web address if they exist

14
Ignore optional parameter in Laravel?

Ignore optional parameter in Laravel?

Currently within my routes file I've got the following Route defined:

29
Laravel: Trying to access value inside array in view template

Laravel: Trying to access value inside array in view template

So I'm trying to access the value of a few pair of key->values inside an array I create and pass to the view called config, I'd like to access to them like this : {{ $config->web_name['value'] }} or {{ $config->$web_name['value'] }} but both are returning...

8
Error on sending e-mail via SMTP

Error on sending e-mail via SMTP

This is my code for sending emailBut i am getting error and page keeps loading for long time

48