ehi*_*ime 1 php oop recursion array-intersect
PHP Catchable fatal error: Argument 2 passed to NUI::recArrInterKey() must be an array, string given我当前在尝试递归地相交多维数组(灰/白列表)时收到错误。
$foo['contact']['im']['provider']由于某种我无法弄清楚的原因,它似乎在该部分上绊倒了?
这是我正在使用的白名单数组 ($array2) 示例
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
)
)
Run Code Online (Sandbox Code Playgroud)
以及方法
/**
* 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;
}
Run Code Online (Sandbox Code Playgroud)
可以说这是$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
)
)
Run Code Online (Sandbox Code Playgroud)
我期待这作为回报
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
)
)
Run Code Online (Sandbox Code Playgroud)
因为您正在使用array_intersect_key,所以它只比较键=>$array1[$key]可能有一个值作为数组,但不是必需的$array2[$key]
您的情况是单向相交:recArrInterKey($a1, $a2)与以下情况不同recArrInterKey($a2, $a1)
尝试这个修复:
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;
}
Run Code Online (Sandbox Code Playgroud)