无法使用其他数组搜索数组键=>值

I am attempting to write a class/method that can take in an array of ids and search a primary array key for the id. If the key is found then search the inner array and make sure none of the other ids exist.

Example:

$ids = array(120, 123, 456, 789);

$primary[120] = array(456,888,99);

$primary[111] = array(789, 886,120);

The output would say that ID-120 is invalid because ID-456 is present.

I've looked at doing a foreach but it's a little too complex for me to comprehend and I feel there must be an easier way to do this? Looking at array_walk perhaps but having no luck making it work.

Here's what I am trying but it's not working...

$ids = array(125,126,182);
    $primary = array();
    $primary['125'] = array(126,301,302,403);

        foreach ($primary as $keyP => $valueP) 
        {
            if(in_array($keyP, $ids)
            {
            echo 'Woops, Primary key found!';
            }
        }

I think the easiest way to do this is to loop through each array with a foreach loop (nesting them) and then evaluating whether it exists using the in_array function. The following code is an example of something you could do:

$ids = array(120, 123, 456, 789,99);

$primary[120] = array(456,888,99);

$primary[111] = array(789, 886,120);

function validateArray($keys, $vals)
{
    foreach ($keys as $key)
    {
        if (isset($vals[$key]))
        {
            foreach ($vals[$key] as $checkId)
            {
                if ( in_array($checkId, $keys) )
                {
                    echo "ID-".$key." is invalid because ID-".$checkId." is present.";
                    // Uncomment if you want to stop evaluating after the first invalidation
                    //return false; 
                }
            }
        }
    }
}
validateArray($ids, $primary);

I ended up with the result ID-120 is invalid because ID-456 is present. when I tested the function. Let me know if this works for you or if you have any questions about how my function works :)

Version 2

Using array_intersect().. eliminates a for loop and, more importantly, in_array().

function validateArray($keys, $vals)
{
    foreach ($keys as $key)
    {
        if (isset($vals[$key]))
        {
            $common = array_intersect($keys, $vals[$key]);
            if ( count($common) > 0)
            {
                echo "ID-".$key." is invalid because ID-".array_shift($common)." is present.";
                // Uncomment if you want to stop evaluating after the first invalidation
                ///return false; 
            }
        }
    }
}

Mmm, what about using in_array() function?

You only assign value if not in the ids array:

foreach ( $primary as $keyP => $valueP ) {
    if ( in_array( $keyP, $ids) {
        echo 'Woops, Primary key found!';
    }
}

So, method above didn't work, what about this?

$ids = array(1, 5, 10);

$flipIds = array_flip($ids);

print_r( $flipIds );

// This ids are ok, so, it shows the position in the original array
echo $flipIds[1]; //  OK, 0 as answer
echo $flipIds[5]; //  OK, 1 as answer
echo $flipIds[10]; // OK, 2 as answer

// Dirty Trick, don't show Notice!
// This shows 'Waps' because it's not in the array (originally a Notice, :P)
if ( @$flipIds[2] ) {
    echo 'ok';
} else {
    echo 'waps';
}

update2

I think it's not working because you're not taking the right values, :P. Try this:

$ids = array(125,126,182);
$primary = array();
$primary['125'] = array(126,301,302,403);

foreach ($primary as $keyP => $valueP) {
    if ( in_array( $keyP, $ids ) {
            echo 'Woops, key From $primary is a Primary key!';
    }
    foreach ($valueP as $id ) {
        if ( in_array($id, $ids) ) {
            echo 'Woops, Primary key found!';
        }
    }
}

This will output 'Woops, Primary key found', as 126 is there, :D