Php 搜索数组,然后用条件反射?
我想做这样的事情:Php 搜索数组,然后用条件反射?,php,arrays,Php,Arrays,我想做这样的事情: $random = array("apple","celery","banana"); $define_fruits = array("apple","banana","pears","mango"); $define_vegetables = array("celery","lettuce","carrots","cucumber"); if($random contains only fruits){ echo fruits; }elsei
$random = array("apple","celery","banana");
$define_fruits = array("apple","banana","pears","mango");
$define_vegetables = array("celery","lettuce","carrots","cucumber");
if($random contains only fruits){
echo fruits;
}elseif($random contains only vegetables){
echo vegetables;
}elseif($random contains both fruits and vegetables){
echo both;
}
已解决:
$in_fruits = sizeof(array_intersect($random,$define_fruits));
$in_vegetables = sizeof(array_intersect($random,$define_vegetables));
if($in_fruits >= 1 && $in_vegetables >= 1){
echo "in_fruits and in_vegetables ";
}elseif($in_fruits === 0 && $in_vegetables >= 1){
echo "in_fruits ";
}elseif($in_fruits >= 1 && $in_vegetables === 0){
echo "in_fruits";
}
进展:
$in_fruits = count(array_intersect($random,$define_fruits));
$in_vegetables = count(array_intersect($random,$define_vegetables));
if( $in_fruits ){
if( $in_vegetables )
echo "both";
else
echo "fruits";
}elseif( $in_vegetables ){
echo "veggies";
}else{
echo "neither ";
您可能需要查看数组_intersect:
您可能需要查看数组\u intersect:
最简单的解决方案是:
if ( in_array($define_fruits, $random) && ! in_array($define_vegetables, $random) ) {
// Only fruits
}elseif ( ! in_array($define_fruits, $random) && in_array($define_vegetables, $random) ) {
// Only Vegetables
}elseif ( in_array($define_fruits, $random) && in_array($define_vegetables, $random) ) {
// Both fruits and vegetables
}
最简单的解决办法是:
if ( in_array($define_fruits, $random) && ! in_array($define_vegetables, $random) ) {
// Only fruits
}elseif ( ! in_array($define_fruits, $random) && in_array($define_vegetables, $random) ) {
// Only Vegetables
}elseif ( in_array($define_fruits, $random) && in_array($define_vegetables, $random) ) {
// Both fruits and vegetables
}
这可能更有效,但这是一个简单的方法:
$random = array("apple","celery","banana");
$define_fruits = array("apple","banana","pears","mango");
$define_vegetables = array("celery","lettuce","carrots","cucumber");
if( count(array_intersect($define_fruits,$random)) ){
if( count(array_intersect($define_vegetables,$random)) )
echo "both";
else
echo "fruit";
} elseif( count(array_intersect($define_vegetables,$random)) ){
echo "vegetables";
} else {
echo "neither";
}
这可能更有效,但这是一个简单的方法:
$random = array("apple","celery","banana");
$define_fruits = array("apple","banana","pears","mango");
$define_vegetables = array("celery","lettuce","carrots","cucumber");
if( count(array_intersect($define_fruits,$random)) ){
if( count(array_intersect($define_vegetables,$random)) )
echo "both";
else
echo "fruit";
} elseif( count(array_intersect($define_vegetables,$random)) ){
echo "vegetables";
} else {
echo "neither";
}
当您使用阵列作为指针时,它将在阵列中查找相同的阵列。当您使用阵列作为指针时,它将在阵列中查找相同的阵列。这如何更有效?它使用数组_相交3次。我的解决方案是使用它两次。我使用你的结构和缓存的变量。谢谢如果您遵循逻辑,数组_intersect总是调用两次,而不是三次。另外,我的代码只有两个比较。你的进步看起来不错。唯一的另一个调整是可能有更好的方法来检查other then count(),PHP文档不清楚,但您可能只需要检查NULL,而不是让count在返回数组时遍历数组。这如何更有效?它使用数组_相交3次。我的解决方案是使用它两次。我使用你的结构和缓存的变量。谢谢如果您遵循逻辑,数组_intersect总是调用两次,而不是三次。另外,我的代码只有两个比较。你的进步看起来不错。唯一的另一个调整是可能有更好的方法来检查other then count(),PHP文档不清楚,但是您可能只需要检查NULL,而不是让count在返回数组时遍历数组。