<?php $aUserFiles = [ 0 => [ 'file' => 'file 1', 'semesters' => [ 3 ] ], 1 => [ 'file' => 'file 2', 'semesters' => [ 3,4 ] ], 2 => [ 'file' => 'file 3', 'semesters' => [ 1,2 ] ], 3 => [ 'file' => 'file 4', 'semesters' => [ 3,4,5,6 ] ] ]; $requiredSemesters = 3; $indicators = []; $result = []; foreach ($aUserFiles as $key => $file) { $first = $file['semesters'][0]; $last = $file['semesters'][count($file['semesters']) - 1]; $filesInRangeCount = count(array_intersect($file['semesters'], range(1, $requiredSemesters))); if (!isset($indicators[$first]) || $indicators[$first]['range'] < $filesInRangeCount) { $indicators[$first] = ["key" => $key, "max" => $last, "range" => $filesInRangeCount]; } } ksort($indicators); $result = []; $max = 0; foreach ($indicators as $indicator) { if ($max >= $requiredSemesters) { break; } $result[] = $aUserFiles[$indicator['key']]; $max = $indicator["max"]; } print_r($result);
You have javascript disabled. You will not be able to edit any code.