Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sj's avatar
Level 3

Filter array and compare plus merge

Hi all,

I'm importing an Excel sheet that looks like this

Meal        food
Breakfast   egg
Breakfast   cheese
dinner      fish
dinner      red wine

And right now i get a array that looks like this

array:30 [
  0 => array:2 [
    "meals" => array:4 [
      "id" => "9e742930-1f24-11ea-a4ce-8c8590a27a7a"
      "meal" => "breakfast"
      "weekdayId" => ""
      "planId" => ""
    ]
    "items" => array:9 [
      "id" => "9e742c64-1f24-11ea-bbfb-8c8590a27a7a"
      "mealId" => "9e742930-1f24-11ea-a4ce-8c8590a27a7a"
      "planId" => ""
      "food" => "egg"
      "grams" => 100
      "protein" => 12
      "carbs" => 136
      "fat" => 9.4
      "note" => null
    ]
  ]
  1 => array:2 [
    "meals" => array:4 [
      "id" => "9e745a9a-1f24-11ea-b70d-8c8590a27a7a"
      "meal" => "breakfast"
      "weekdayId" => ""
      "planId" => ""
    ]
    "items" => array:9 [
      "id" => "9e745cb6-1f24-11ea-9a41-8c8590a27a7a"
      "mealId" => "9e745a9a-1f24-11ea-b70d-8c8590a27a7a"
      "planId" => ""
      "food" => "cheese"
      "grams" => 20
      "protein" => 5.74
      "carbs" => 52.4
      "fat" => 3.2
      "note" => null
    ]
  ]
.....

I need to somehow check if there are more of the same meals and then merge them. So I only got 1 meals and then all the items that has the same mealId

Right now my import method looks like this

    public function import()
    {
        request()->file('file')->storeAs('excel', auth()->id().'.xlsx');

        $path = storage_path('app/excel/'.auth()->id().'.xlsx');

        $rows = SimpleExcelReader::create($path)->getRows();

        $meals = [];
        $rows->each(function (array $rowProperties) use (&$meals) {
            $mealId  = Uuid::uuid1()->toString();
            $meals[] = [
                'meals' => [
                    'id' => $mealId,
                    'meal' => $rowProperties['Måltid'],
                    'weekdayId' => '',
                    'planId' => '',
                ],

                'items' => [
                    'id' => Uuid::uuid1()->toString(),
                    'mealId' => $mealId,
                    'planId' => '',
                    'food' => $rowProperties['Fødevare'],
                    'grams' => $rowProperties['Mængde (g)'],
                    'protein' => $rowProperties['Protein (g)'],
                    'carbs' => $rowProperties['Energi (kcal)'],
                    'fat' => $rowProperties['Fedt (g)'],
                    'note' => null,
                ],
            ];
        });

        dd($meals);
    }

Hope someone can guide me in the right direction 🙏

0 likes
2 replies
tykus's avatar

I don't know what format you are looking for, but you can group the Collection by any of its (nested) attributes, e.g.

// your existing code
$rows->each(function (array $rowProperties) use (&$meals) {
    $mealId = Uuid::uuid1()->toString();
    $meals[] = [
        'meals' => [
            'id' => $mealId,
            'meal' => $rowProperties['Måltid'],
            'weekdayId' => '',
            'planId' => '',
        ],

        'items' => [
            'id' => Uuid::uuid1()->toString(),
            'mealId' => $mealId,
            'planId' => '',
            'food' => $rowProperties['Fødevare'],
            'grams' => $rowProperties['Mængde (g)'],
            'protein' => $rowProperties['Protein (g)'],
            'carbs' => $rowProperties['Energi (kcal)'],
            'fat' => $rowProperties['Fedt (g)'],
            'note' => null,
        ],
    ];
})
// grouping
->groupBy('meals.id');

Now you will have a Collection grouped by the UUID, where each group contains the items associated with that meal - it can be further by adding to the Collection pipeline

sj's avatar
Level 3

Thanks a lot for you answer @tykus But it looks like I just get the same output 🤔

Please or to participate in this conversation.