0

我有两个数组 $schoolteachers 和 $schoolgroups ......

$schoolgroups=Array ([0] = Array ([groupid] = 1 [groupname] =Red [members] = Array ([0] = Array ([id] = 13 [name] = Sooraj)

                [1] = Array
                    (
                        [id] = 12
                        [name] = sanjay
                    )

            )

    )

[1] => Array
    (
        [groupid] = 2
        [groupname] = Blue
        [members] = Array
            (
                [0] = Array
                    (
                        [id] = 9
                        [name] = Anith
                    )

                [1] = Array
                    (
                        [id] = 4
                        [name] = John
                    )

            ); 

$schoolteachers=数组(

[0] => Array
    (
        [employee_id] = 7
        [emp_name] = Anantha Raman
    )

[1] => Array
    (
        [employee_id] => 9
        [emp_name] = Anith
    )

[2] = Array
    (
        [employee_id] = 11
        [emp_name] = Aravind
    ) });

我想检查 $schoolteachers 'empname',is in schoolgroup array($schoolgroup ['members'][''name]=Aravind) 并且只回显非成员我想显示基于名称的 $schoolteachers 数组,如果它不存在于$schoolgroups

我用的代码不好

foreach ($schoolteachers as $teachers) {
    $classin = false;
    foreach (new RecursiveIteratorIterator(new RecursiveArrayIterator($schoolgroups)) as $value) {
        if ($value == $teachers['emp_name']) {

            $classin = true;
            break;
        }
    }
    if (!$classin) {
        echo $teachers['emp_name'];
    }
}
4

1 回答 1

1

你可以试试

$names = array_map(function($v){ return $v['emp_name']; }, $schoolteachers);
$members = array();
array_walk_recursive($schoolgroups, function($item,$key) use (&$members) {
    $key === "name" and $members[] = $item ;
});

echo "<pre>";
foreach($names as $name)
{
    if(!in_array($name, $members))
        echo $name,PHP_EOL;

}

输出

Anantha Raman
Aravind

查看完整演示

于 2012-12-07T06:44:37.513 回答