Filter Array Based on Another Array Key and Values

ⅰ亾dé卋堺 提交于 2019-12-14 02:24:30

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!