1

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

0

3 Answers 3

3
$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();
Sign up to request clarification or add additional context in comments.

Comments

1

You can key the collection by dates and then add in the dates that are not in there:


$out = $collection->keyBy('date')
$out = $out->merge(
    collect($dates)
        ->whereNotIn(null, $out->keys())
        ->mapWithKeys(fn ($date) => [
            $date => [
                'new' => 0,
                'settled' => 0,
                'date' => $date
            ]
        ])
)->sortKeys()->values();

What this does is convert the collection to one that has the dates as keys, then filters from dates all the dates that are already in the collection, maps the rest into your desired format, sorts them all to how they should be then discards the keys.

2 Comments

your code produces an error, it lacks a single close parenthesis and if I add one, it says Too few arguments to function Illuminate\Support\Collection::whereNotIn(), can you format it a bit cleaner, Im confuse the way you call the merge and whereNotIn
@SymmetricsWeb I've updated the code. It needed the "key" parameter
0

For the $dates maybe you can filter only the dates that does not exist in the collections? Then after you can map them out and merge.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.