根据另一个数组键和值过滤数组 [英] Filter Array Based on Another Array Key and Values

查看:75
本文介绍了根据另一个数组键和值过滤数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数组1中有2个数组
我有技能并且其合格分数

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
                )

        )

)

我想基于数组1过滤学生

i want to filter the student based on array 1

我想招收学生

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

推荐答案

使用以下方法:

$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);

输出:

Array
(
    [0] => 24
)

测试链接: https://eval.in/private/10a7add53b1378

这篇关于根据另一个数组键和值过滤数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆