我试图检查一个多维数组并只保留它们匹配的键并且具有值(不是相同的值)。
所以我想检查整个数组并找回它们有价值的密钥,在所有数组中检查它们,只保留所有数组包含的密钥。
所以这是我的数组:
Array
(
[0] => Array
(
[id] => 21
[tstamp] => 1508482179
[firstname] => test1
[lastname] => test1
[dateOfBirth] =>
[gender] =>
[company] =>
[street] =>
[postal] =>
[city] =>
[state] =>
[country] =>
[phone] =>
[mobile] =>
[fax] =>
[email] => test1@test.com
[website] =>
[language] =>
[groups] => a:1:{i:0;s:1:"1";}
[login] => 1
[username] => test2
[assignDir] =>
[homeDir] =>
[disable] =>
[start] =>
[stop] =>
[dateAdded] => 1508482142
[lastLogin] => 0
[currentLogin] => 0
[loginCount] => 3
[locked] => 0
[session] =>
[createdOn] => 0
)
[1] => Array
(
[id] => 2
[tstamp] => 1508482189
[firstname] => test2
[lastname] => test2
[dateOfBirth] =>
[gender] =>
[company] =>
[street] =>
[postal] =>
[city] =>
[state] =>
[country] =>
[phone] =>
[mobile] =>
[fax] =>
[email] => test2@test.com
[website] =>
[language] =>
[groups] => a:1:{i:0;s:1:"1";}
[login] => 1
[username] => test2
[assignDir] =>
[homeDir] =>
[disable] =>
[start] =>
[stop] =>
[dateAdded] => 1508482142
[lastLogin] => 0
[currentLogin] => 0
[loginCount] => 3
[locked] => 0
[session] =>
[createdOn] => 0
)
)
代码:
$arrResultMap = array_map('array_filter', $arrResult);
$currencies = count($arrResultMap) > 1 ? call_user_func_array('array_intersect', $arrResultMap) : array_shift($arrResultMap);
print_r($currencies);
输出:
Array
(
[id] => 1
[groups] => a:1:{i:0;s:1:"1";}
[login] => 1
[loginCount] => 3
)
如你所见,有一些关键缺失。像电子邮件,名字,姓氏,tstamp等我无法看到或发现我做错了什么:)
更新: 找到解决方案而不是使用array_intersect我使用array_intersect_key。 现在它有效。
此代码工作:
$arrResultMap = array_map('array_filter', $arrResult);
$currencies = count($arrResultMap) > 1 ? call_user_func_array('array_intersect_key', $arrResultMap) : array_shift($arrResultMap);
print_r($currencies);