我有一个数组
array('apples', 'oranges', 'grapes', 'watermelons', 'bananas');
如果只有苹果和橘子,我不想打印这个数组。我该怎么做?
答案 0 :(得分:4)
您可以查看此example here:
$haystack = array(...);
$target = array('foo', 'bar');
if(count(array_intersect($haystack, $target)) == count($target)){
// all of $target is in $haystack
}
答案 1 :(得分:2)
拿出苹果和橘子,看看是否还有什么东西。
$arr = array('apples', 'oranges', 'grapes', 'watermelons', 'bananas');
$arrDiff = array_diff($arr, array('apples', 'oranges')); //take out the apples and oranges
if(!empty($arrDiff)) //there's something other than apples and oranges in the array
print_r($arr);
答案 2 :(得分:1)
if (in_array('apples', $array) && in_array('oranges', $array) && count($array) == 2)
{
// Don't print array
}
答案 3 :(得分:0)
如果您事先知道要进入数组的元素数量,那么您可以这样做:
<?php
$expectedCount = 5; //in this example, we are looking for five elements in our array
if(count($array) == $expectedCount)
{
var_dump($array);
}
答案 4 :(得分:0)
首先指定数组必须具有的最小元素量,例如在这种情况下为5 然后使用函数count(),可以在http://php.net/manual/en/function.count.php
中找到引用if(count(array) >= 5)
{
//perform action
}