Home > Back-end >  how I can filter my array to return the first element or the next
how I can filter my array to return the first element or the next

Time:01-08

I'm using Laravel 9 and I have a multidimensional array of topics & lessons I would like to return the first lesson of the topic if recentCompletedLesson id equal the id of the last lesson of a topic. Otherwise move to the next lesson:

$lessons = [
            'topics' => [
                [
                    'lessons' => [
                        [
                            "id" => 1,
                            "name" => "HTML"
                        ],
                        [
                            "id" => 2,
                            "name" => "PHP"
                        ]
                    ],
                    "recentCompletedLesson" => [
                        "id" => 2,
                        "completedAt" => "2023-01-01"
                    ]
                ],
                [
                    'lessons' => [
                        [
                            "id" => 3,
                            "name" => "RUBY"
                        ],
                        [
                            "id" => 4,
                            "name" => "JAVA"
                        ]
                    ],
                    "recentCompletedLesson" => [
                        "id" => 3,
                        "completedAt" => "2023-01-01"
                    ]
                ]
            ]
        ];

Expected result :

[
   [
     "id" => 1,
     "name" => "HTML"
   ],
   [
     "id" => 4,
     "name" => "JAVA"
   ]
]

I tried :

$results = [];
foreach ($lessons['topics'] as $topic) {
    $lessonIds = array_column($topic['lessons'], 'id');
    $lastLessonId = end($lessonIds);
    if ($topic['recentCompletedLesson']['id'] == $lastLessonId) {
        // The next lesson is the first lesson of the next topic
        continue;
    }

    // Find the next lesson
    $nextLessonId = $topic['recentCompletedLesson']['id']   1;
    foreach ($topic['lessons'] as $lesson) {
        if ($lesson['id'] == $nextLessonId) {
            // This is the next lesson
            $results[] = $lesson['name'];
            break;
        }
    }
}

return $results;

CodePudding user response:

$results = [];
foreach ($lessons['topics'] as $topic) {
  $recentCompletedLesson = $topic['recentCompletedLesson']['id'];
  $topicLessons = $topic['lessons'];
  $lessonLen = count($topicLessons);
   if ($topicLessons[$lessonLen - 1]['id'] == $recentCompletedLesson) {
      $results[] = $topicLessons[0];
   } else {
      $results[] = $topicLessons[$lessonLen - 1];
   }
}
return $results;

Hopefully, it will work for as you describe.

CodePudding user response:

$firstLessons = [];
        foreach ($lessons['topics'] as $topic) {
            $lastLessonId = end($topic['lessons'])['id'];
            if ($topic['recentCompletedLesson']['id'] == $lastLessonId) {
                $firstLessons[] = reset($topic['lessons']);
            } else {
                $filteredLessons = array_filter($topic['lessons'], function($lesson) use ($topic) {
                    return $lesson['id'] != $topic['recentCompletedLesson']['id'];
                });
                $firstLessons = array_merge($firstLessons, $filteredLessons);
            }
        }

        return $firstLessons;

Or clean version, here I used continue to avoid else statement, for a clean and readable code

$firstLessons = [];
    foreach ($lessons['topics'] as $topic) {
        $lastLessonId = end($topic['lessons'])['id'];
        if ($topic['recentCompletedLesson']['id'] == $lastLessonId) {
            $firstLessons = array_merge($firstLessons, array_slice($topic['lessons'], 0, $matrix[0][0]));
            continue;
        }
        // $i = 0; and put $i in $matrix[0][$i]
        $filteredLessons = array_filter($topic['lessons'], function($lesson) use ($topic) {
            return $lesson['id'] != $topic['recentCompletedLesson']['id'];
        });
        $firstLessons = array_merge($firstLessons, $filteredLessons);
    }

    dump($firstLessons);
  •  Tags:  
  • php
  • Related