0

i have two arrays $schoolteachers and $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=Array (

[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
    ) });

i want to check $schoolteachers 'empname',is in schoolgroup array($schoolgroup ['members'][''name]=Aravind) and echo only non members i want to display name based $schoolteachers array if it is not present in $schoolgroups

I used the code it's not fine

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
  • It would be nice to have structure of your array .... and sample expected output Commented Dec 7, 2012 at 6:41
  • hi i add the array to my question Commented Dec 7, 2012 at 6:59
  • What is your expected Result ?? Did you test the code below ? Commented Dec 7, 2012 at 7:07
  • i want to echo employe names(ie,Anantha Raman ,Aravind) from the array schoolteachers here Anith is exists in schoolgroups therefor it don't echo Commented Dec 7, 2012 at 7:15

1 Answer 1

1

You can try

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

}

Output

Anantha Raman
Aravind

See Full Demo

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.