[英]PHP: Check if array values exist in another array
i have created a array like this :我创建了一个这样的数组:
$foods_customer = array(["meal"=>1,"rice"=>1];
when i return this array , looks like :当我返回这个数组时,看起来像:
[{meal: 1, rice: 1}]
0: {meal: 1, rice: 1}
meal: 1
rice: 1
and i have another array like this :我有另一个这样的数组:
$foods , when i return , looks like this . $foods ,当我回来时,看起来像这样。
[{meal: 1}, {meal: 1, rice: 1}]
0: {meal: 1}
meal: 1
1: {meal: 1, rice: 1}
meal: 1
rice: 1
i wanna check if array $foods_customer , exist inside array $foods.我想检查数组 $foods_customer 是否存在于数组 $foods 中。
in this case , must return true , bit if $foods_customer was like this :在这种情况下,必须返回 true ,如果 $foods_customer 是这样的:
[{meal: 1, rice: 1 , chesse:1}]
0: {meal: 1, rice: 1,chesse:1}
meal: 1
rice: 1
chesse:1
so , return false .所以,返回 false 。
You can do it very easily with just foreach and Identical checking.只需使用 foreach 和 Identical 检查,您就可以非常轻松地做到这一点。 First make a loop of your main array then make another loop inside of it and then check the array identically.
首先对主数组进行循环,然后在其中进行另一个循环,然后以相同的方式检查数组。
Try this out:试试这个:
$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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.