PHP:检查另一个数组中是否存在数组值



我创建了一个这样的数组:

$foods_customer = array(["meal"=>1,"rice"=>1];

当我返回这个数组时,看起来像:

[{meal: 1, rice: 1}]
0: {meal: 1, rice: 1}
meal: 1
rice: 1

我有另一个这样的数组:

当我回来的时候,$foods看起来是这样的。

[{meal: 1}, {meal: 1, rice: 1}]
0: {meal: 1}
meal: 1
1: {meal: 1, rice: 1}
meal: 1
rice: 1

我想检查数组$foods_customer是否存在于数组$foods中。

在这种情况下,必须返回true,如果$foods_customer是这样的话:

[{meal: 1, rice: 1 , chesse:1}]
0: {meal: 1, rice: 1,chesse:1}
meal: 1
rice: 1
chesse:1

因此,返回false。

只需foreach和相同检查就可以轻松完成。首先对主数组进行循环,然后在其中进行另一个循环,然后对数组进行相同的检查。

试试这个:

$foods_customer = array(["meal"=>1,"rice"=>1]);
$foods  = array(["meal"=>1 ],["meal"=>1,"rice"=>1]);
$res = false;
foreach( $foods as $food ){
foreach( $foods_customer as $fc ){
if( $fc === $food ){
$res  = true;
break;
}
}
}
echo $res;

最新更新