Search code examples
phplaravellaravel-collection

Laravel collection merge from array based on item key value


I have an array of $dates like below

[
    "2022-30",
    "2022-31",
    "2022-32",
    "2022-33",
    "2022-34",
    "2022-35",
]

and I have a $collection with an output like below

[
    {
        "new": 60,
        "settled": "1",
        "date": "2022-31"
    },
    {
        "new": 50,
        "settled": "1",
        "date": "2022-32"
    },
]

how can I achieve a result like below which merge the value of date from the $collection item if it matches from the date on $dates array

[
    {
        "new": 0,
        "settled": "0",
        "date": "2022-30"
    },
    {
        "new": 60,
        "settled": "1",
        "date": "2022-31"
    },
    {
        "new": 50,
        "settled": "1",
        "date": "2022-32"
    },
    {
        "new": 0,
        "settled": "0",
        "date": "2022-33"
    },
    {
        "new": 0,
        "settled": "0",
        "date": "2022-34"
    },
    {
        "new": 0,
        "settled": "0",
        "date": "2022-35"
    }
]

I tried making the dates into a collection and formatting the output like the $collection format and use merge but this just combine the two collection together with duplicates.

$out = collect($dates)->map( function($d, $k)  {
    return [
        'new' => 0,
        'settled' => 0,
        'date' => $d
    ];
});

return $out->merge($collection);

appreciate any help


Solution

  • $dates = [
        "2022-30",
        "2022-31",
        "2022-32",
        "2022-33",
        "2022-34",
        "2022-35",
    ];
    
    $collection = collect([
        [
            "new" => 60,
            "settled" => "1",
            "date" => "2022-31"
        ],
        [
            "new" => 50,
            "settled" => "1",
            "date" => "2022-32"
        ]
    ]);
    
    foreach ($dates as $date) {
        if (!$collection->where('date', $date)->count()) {
            $collection->push([
                    'new' => 0,
                    'settled' => 0,
                    'date' => $date
            ]);
        }
    }
    
    return $collection->sortBy('date')->values()->all();