Php 比较数组与非数组值
我尝试将数组中的一个值与另一个值进行比较。但是,即使我有两个不同的值(一个是10,数组中的其他值是4和6),该消息也仅在数组2中显示退出 给出的值为Php 比较数组与非数组值,php,arrays,Php,Arrays,我尝试将数组中的一个值与另一个值进行比较。但是,即使我有两个不同的值(一个是10,数组中的其他值是4和6),该消息也仅在数组2中显示退出 给出的值为 array(2) { [0]=> array(2) { ["post_ID"]=> string(1) "4" [0]=> string(1) "4" } [1]=> array(2) {
array(2) { [0]=> array(2) { ["post_ID"]=> string(1) "4"
[0]=> string(1) "4"
}
[1]=> array(2) { ["post_ID"]=> string(1) "6"
[0]=> string(1) "6"
}
}
我不认为in_数组会递归检查,,,可能有更好的代码。。但我的解决办法是
$bool = false;
foreach($orderP as $order){
if (in_array($post,$order)){
echo ' exists in array2';
$bool=true;
return;
}
}
if(!$bool){
echo 'does not exists in array2';
}
您有多维数组,所以请尝试以下操作
$ids = array_column($orderP, 'post_ID');
if (in_array($post,$ids)){
echo ' exists in array2';
}
else{
echo 'does not exists in array2';
}
in_数组本身将为false或true,因此只要(in_数组($post,$orderP)){echo'存在';}为enough@vSugumar好的,我试图删除==0,显然它仍然不存在,即使我有一个匹配值4和数组(4),检查我下面的答案,告诉我它是否有帮助Hanks,它有帮助。您的和上面的代码:)
$ids = array_column($orderP, 'post_ID');
if (in_array($post,$ids)){
echo ' exists in array2';
}
else{
echo 'does not exists in array2';
}