第一个数组是$niz
:
Array (
[Swansea] => 4
[Stoke City] => 3
[Sunderland] => 3
[Southampton] => 5
[Liverpool] => 3
[Manchester United] => 2
[Hull City] => 1
[Tottenham] => 2
[Newcastle Utd] => 1
[Aston Villa] => 1
[West Ham] => 2
[Crystal Palace] => 3
[Chelsea] => 3
)
第二个数组是$ niz1:
Array (
[Stoke City] => 2
[Sunderland] => 2
[Liverpool] => 1
[Hull City] => 1
[Tottenham] => 1
[Manchester United] => 1
[Newcastle Utd] => 1
[Crystal Palace] => 3
[Chelsea] => 1
)
如何组合这些数组以获取$niz2
(键的顺序类似于数组$niz1
,值来自匹配的数组$niz
),如:
$niz2
:
Array (
[Stoke City] => 3
[Sunderland] => 3
[Liverpool] => 3
[Hull City] => 1
[Tottenham] => 2
[Manchester United] => 2
[Newcastle Utd] => 1
[Crystal Palace] => 3
[Chelsea] => 3
)
我尝试使用函数array_merge()
,但我得到了空值,并尝试使用array_intersect_key()
。
答案 0 :(得分:2)
试试这个
foreach ($niz1 as $k=>$n)
{
if(in_array($k,$niz1))
{
$niz2[$k]=$niz[$k];
}
}
print_r($niz2);
答案 1 :(得分:1)
试试这个:
$temp = array_intersect_key($niz, $niz1);
foreach ($niz1 as $k => $v) {
$niz2[$k] = $temp[$k];
}
答案 2 :(得分:1)
//(overwrites the values of $niz1 with those of $niz2)
$bif=array_merge($niz1,$niz);
//(removes everything from $bif that is not in $niz1)
$result=array_intersect($niz1,$bif);