I have an array like
$a=array([0]=>0 [1]=>3)
$b=array([0]=>image [1]=>profile [2]=>password [3]=>login)
我想比较数组a的键值,即0与数组b的索引值0。
$a = array(0, 3);
$b = array(0 => 'image', 1 => 'profile', 2 => 'password', 3 => 'login');
$c = array_intersect_key($b, array_flip($a));
结果
Array
(
[0] => image
[3] => login
)
将inarray用于foreach循环
<?php
$a = array(0,3);
$b= array('image','profile','password','login');
foreach($b as $key=>$value){
if(in_array($key, $a)) {
echo $value."<br>";
}
}
?>
输出
image
login
array_intersect($a,$b);
或者尝试使用 ===
运算符来比较数组中的值
<?php
$a=array(0,3);
$b=array(image,password);
foreach($a as $k=>$v){
if($a[$k]===$b[$k]){
echo "$k index is Same<br>";
}else{
echo "$k index is different<br>";
}
}
输出
0 index is different
1 index is different