0

I am using in_array to detect if a value is within a PHP array, my array looks like this...

$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?

0

6 Answers 6

2

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:

$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!';
}
Sign up to request clarification or add additional context in comments.

2 Comments

This only works for the first array though, how about if the array has only grapes but more than one of them?
Well, that's not what the question asked at first. It says 'is the only item in the array'
1

To display custom message only when 'grape' is the only fruit in the list you can do it by changing your code to:

/* 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';
}

Comments

1

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. If "grape" is the only item in the array, the result of array_unique($fruits) should be equal to array('grape'). The === operator checks that both values are arrays and they both have the same elements.

Comments

0

Try this,

Using array_count_values in-built function.

<?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 />";
}
?>

Comments

-1

You can use the function 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.

Hopefully it helps you out.

Comments

-1

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.