我有两个数组,我需要在数组相同的Key时获取内容
我使用array_intersect_key
获取相同的密钥并返回此结果:
Array1 :
(
[NewYork] = "5,NewYork";
[london] = "20,london";
[Pari] = "40,Pari";
)
Array2 :
(
[China] = "14,China";
[london] = "40,london";
[Tokyo] = "2,Tokyo";
)
结果
array_intersect_key(Array1,Array2);
它返回:[london] = "20,london";
但我需要展示[london] = "20,london"; and [london] = "40,london";
或类似的内容[london] = "20,london|40,london";
没有循环我有一个大文件
谢谢!
答案 0 :(得分:2)
您好我已经尝试过这段代码来获得没有循环的最接近的结果。这可能会对你有所帮助。
$array1['NewYork'] = "5,NewYork";
$array1['london'] = "20,london";
$array1['Tokyo'] = "40,Pari";
$array2['China'] = "14,China";
$array2['london'] = "40,london";
$array2['Tokyo'] = "2,Tokyo";
$res1 = array_intersect_key($array1,$array2);
$res2 = array_intersect_key($array2,$array1);
echo "<pre>";
print_r(array_merge_recursive($res1,$res2));
die;
Array
(
[london] => Array
(
[0] => 20,london
[1] => 40,london
)
[Tokyo] => Array
(
[0] => 40,Pari
[1] => 2,Tokyo
)
)
让我知道它是否有效。祝你好运。
这个怎么样?它将返回相同的结果。
function myfunction($a,$b)
{
if ($a!=$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
$array1['NewYork'] = "5,NewYork";
$array1['london'] = "20,london";
$array1['Tokyo'] = "40,Pari";
$array2['China'] = "14,China";
$array2['london'] = "40,london";
$array2['Tokyo'] = "2,Tokyo";
$res1 = array_merge_recursive($array1,$array2);
$res2 = array_uintersect_assoc($res1,$array1,"myfunction");
echo "<pre>";
print_r($res2);
die;