问题
i have 2 arrays in array 1 i have skill and its eligible marks
Array
(
[3] => 2 // skill => eligible marks
[63] => 6
[128] => 3
)
in array to i have student and its skill and obtained marks
Array
(
[22] => Array
(
[0] => Array
(
[skill_id] => 3
[gd_score] => 4
)
[1] => Array
(
[skill_id] => 128
[gd_score] => 6
)
)
[23] => Array
(
[0] => Array
(
[skill_id] => 128
[gd_score] => 3
)
)
[24] => Array
(
[0] => Array
(
[skill_id] => 3
[gd_score] => 7
)
[1] => Array
(
[skill_id] => 63
[gd_score] => 8
)
[2] => Array
(
[skill_id] => 128
[gd_score] => 9
)
)
)
i want to filter the student based on array 1
i want to get student
with skill 3 and marks grater than 2
AND skill 63 and marks grater than 6
AND skill 128 and marks grater than 3
if criteria Stratifies return student id
回答1:
Use the following approach:
$marks = array
(
3 => 2, // skill => eligible marks
63 => 6,
128 => 3
);
// $arr is your initial array of student data
$student_ids = [];
$marks_count = count($marks);
foreach ($arr as $k => $items) {
// if number of marks coincide
if (count($marks) != count($items)) continue;
foreach ($items as $item) {
if (!isset($marks[$item['skill_id']])
|| $marks[$item['skill_id']] >= $item['gd_score']) {
continue 2;
}
}
$student_ids[] = $k;
}
print_r($student_ids);
The output:
Array
(
[0] => 24
)
Test link: https://eval.in/private/10a7add53b1378
来源:https://stackoverflow.com/questions/41554606/filter-array-based-on-another-array-key-and-values