Question

I'm currently receiving an error PHP Catchable fatal error: Argument 2 passed to NUI::recArrInterKey() must be an array, string given when trying to recursively intersect a multidimensional array (grey/white list).

It seems to be tripping on the $foo['contact']['im']['provider'] section for some reason that I can't figure out?

Here's an example whitelist array ($array2) that I am using

Array
(
    [location] => false
    [network_name] => false
    [interests] => false
    [last_name] => false
    [url] => false
    [significant_other] => false
    [network_domains] => false
    [contact] => Array
        (
            [im] => Array
                (
                    [provider] => false
                )

            [email_addresses] => false
        )

)

And the method

/**
 * Recursive array intersect key
 */
private static function recArrInterKey(array $array1, array $array2) 
{
    $array1 = array_intersect_key($array1, $array2);
    foreach ($array1 as $key => &$value) 
    {
        if (is_array($value)) 
        {
            $value = self::recArrInterKey($value, $array2[$key]);
        }
    }
    return $array1;
}

lets say that this is $array1

Array
(
    [location] => Seattle
    [occupation] => Developer
    [network_name] => foo.network
    [network_region] => foo.region
    [interests] => coding
    [last_name] => daniel
    [url] => false
    [id] => 4665228
    [significant_other] => some girl
    [network_domains] => false
    [contact] => Array
        (
            [im] => Array
                (
                    [provider] => aol
                )
            [phone] => Array
                (
                    [provider] => at&t
                )

            [email_addresses] => dont@mail.me
        )

)

I am expecting this as a return

Array
(
    [location] => Seattle
    [occupation] => Developer
    [network_name] => foo.network
    [interests] => coding
    [last_name] => daniel
    [url] => false
    [significant_other] => some girl
    [network_domains] => false
    [contact] => Array
        (
            [im] => Array
                (
                    [provider] => aol
                )
            [email_addresses] => dont@mail.me
        )

)
Was it helpful?

Solution 2

well - you dont check the second parameter: $array2[$key] - this also should be an array

OTHER TIPS

Because you are using array_intersect_key, it only compare keys => $array1[$key] might have a value as array but not necessary $array2[$key]

You case is a one direction intersect: recArrInterKey($a1, $a2) is not the same as recArrInterKey($a2, $a1)

Try this fix:

private static function recArrInterKey(array $array1, array $array2) 
{
    $array1 = array_intersect_key($array1, $array2);
    foreach ($array1 as $key => &$value) 
    {
        if (is_array($value)) 
        {
            $value = is_array($array2[$key]) ? self::recArrInterKey($value, $array2[$key]) : $value;
        }
    }
    return $array1;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top