[英]PHP - Check if values in one array exist in another array multi-dimensional array
[英]PHP: Check if array values exist in another array
我創建了一個這樣的數組:
$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 和 Identical 檢查,您就可以非常輕松地做到這一點。 首先對主數組進行循環,然后在其中進行另一個循環,然后以相同的方式檢查數組。
試試這個:
$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.