简体   繁体   English

PHP in_array(如果只有此项)

[英]PHP in_array if only item

I am using in_array to detect if a value is within a PHP array, my array looks like this... 我正在使用in_array来检测值是否在PHP数组中,我的数组看起来像这样...

$fruits = array("banana", "grape", "orange", "apple");

/* is grape in the array */
if (in_array('grape', $fruits)) {
    echo 'Grape Detected';
} else {
    echo 'Grape not detected';
}

I am trying to modify this so that it can also detect when the 'grape' is the only item in the array, so if the array looked like this... 我正在尝试对此进行修改,以便它也可以检测到“葡萄”何时是数组中的唯一项,因此,如果数组看起来像这样……

$fruits = array("grape", "grape", "grape");

or... 要么...

$fruits = array("grape");

Then I could display a custom message, anyone have an example I can see? 然后,我可以显示一条自定义消息,有人可以看到示例吗?

Check if there isn't more than one element, and if it matches your condition. 检查是否有不止一个元素,并且是否符合您的条件。

if(count($fruits) === 1 && in_array('grape', $fruits)) {
    echo "There's only one fruit here, and it's a grape!";
}

EDIT: 编辑:

You can check if 'grape' is the only thing in the array and also how many of it are there that way: 您可以检查“ grape”是否是数组中唯一的东西,以及通过这种方式有多少个葡萄:

$condition_met = false;
foreach ($fruits as &$iterator) {
    if($iterator !== 'grape') {
        $condition_met = true;
    }
}

if($condition_met === false)
{
    echo 'There are only grapes in this fruits basket! There are ' . count($fruits) . ' unique beauties!';
}

To display custom message only when 'grape' is the only fruit in the list you can do it by changing your code to: 要仅在列表中只有“ grape”(水果)时显示自定义消息,可以通过将代码更改为:

/* is grape in the array */
if (in_array('grape', $fruits)) {
    if (count(array_unique($fruits)) === 1) {
        echo 'Grape is the only fruit in the list';
    } else {
        echo 'Grape detected';
    }
} else {
    echo 'Grape not detected';
}

Here is the simplest way to do that: 这是最简单的方法:

if (array_unique($fruits) === array('grape')) {
    echo 'Grape Detected';
}

Explanation: array_unique removes all duplicate values from an array. 说明: array_unique从数组中删除所有重复的值。 If "grape" is the only item in the array, the result of array_unique($fruits) should be equal to array('grape') . 如果“ grape”是数组中的唯一项,则array_unique($fruits)的结果应等于array('grape') The === operator checks that both values are arrays and they both have the same elements. ===运算符检查两个值都是数组,并且它们都具有相同的元素。

Try this, 尝试这个,

Using array_count_values in-built function. 使用array_count_values内置函数。

<?php
$fruits = array("banana", "grape", "orange", "apple","grape", "grape",     "grape");
$tmp_fruits = array_count_values($fruits);
/* is grape in the array */
foreach($tmp_fruits as $fruit=>$total){
    echo $fruit." Detected ".$total." Times."."<br />";
}
?>

You can use the function array_count_values() . 您可以使用函数array_count_values() This will return a new array with the number of times an item was repeated. 这将返回一个新数组,其中包含重复项的次数。

<?php
    $fruits = array("grape", "grape", "grapr");

    $vals = array_count_values($fruits);
    echo 'Unique Items: '.count($vals).'<br><br>';
    print_r($vals);
?>

Will output: 将输出:

Unique Items: 2

Array ( [grape] => 2 [grapr] => 1 ) //Shows the item in the array and how many times it was repeated

You can then loop through the new array, $vals , to find out how many times an item was repeated and display the corresponding message. 然后,您可以遍历新数组$vals ,以找出重复某项的次数并显示相应的消息。

Hopefully it helps you out. 希望它可以帮助您。

Here's a check that is true if: 如果满足以下条件,则此检查为真:

  • Array only has 'Grape' 数组只有“葡萄”
  • Regardless of whether or not there are multiple 'Grape' entries 不管是否有多个“葡萄”条目
$uniqueFruits = array_unique($fruit);

if (count($uniqueFruits) == 1 && $uniqueFruits[0] == 'grape') {
     // Only 'grape' in here
} elseif() {
  // Some other check
} else {
  // Otherwise
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM