2

I have an array with 2 possible values negative and positive. If all the values are positive my function has to return positive.

If all values are negative my function has to return negative. If the values are a mix then my function has to return partly.

My code always returns partly, unfortunately, I don't know why.

 const RESULT_OF_SEARCH_POSITIVE = "positive";
    const RESULT_OF_SEARCH_NEGATIVE = "negative";
    const RESULT_OF_SEARCH_PARTLY = "partly";

    ...

private function calculateResultOfSearch(array $imagesResultArray)
  {
   if (array_unique($imagesResultArray) === self::RESULT_OF_SEARCH_POSITIVE) {
       return self::RESULT_OF_SEARCH_POSITIVE;
   } elseif(array_unique($imagesResultArray) === self::RESULT_OF_SEARCH_NEGATIVE) 
     {
       return self::RESULT_OF_SEARCH_NEGATIVE;
   } else {
       return self::RESULT_OF_SEARCH_PARTLY;
    }
}
5
  • How is count, which returns an integer supposed to be equal to a String with the value of positive or negative? Commented Nov 21, 2018 at 9:21
  • @maio290 my bad I am retarded I think I updated OP Commented Nov 21, 2018 at 9:23
  • Possible duplicate of php How to check if an array of numbers are all positive Commented Nov 21, 2018 at 9:25
  • @Jean-MarcZimmer no it's not a duplicate, my question has nothing to do with numbers and there are 3 conditions not 1 Commented Nov 21, 2018 at 9:26
  • Oh, OK, sorry. I though you were talking of positive and negative values ! My bad. Commented Nov 21, 2018 at 9:26

5 Answers 5

3

As we know the count() function always returns the count of the array. So it goes to the else case in every match of the condition.

You should try something like this:

class Demo{
    const RESULT_OF_SEARCH_POSITIVE = "positive";
    const RESULT_OF_SEARCH_NEGATIVE = "negative";
    const RESULT_OF_SEARCH_PARTLY = "partly";

function calculateResultOfSearch(array $imagesResultArray)
{
  if (count(array_count_values($imagesResultArray)) == 1 && $imagesResultArray[0] === self::RESULT_OF_SEARCH_POSITIVE) {
   return current($imagesResultArray);
} elseif(count(array_count_values($imagesResultArray)) == 1 && $imagesResultArray[0]== self::RESULT_OF_SEARCH_NEGATIVE) {
  return self::RESULT_OF_SEARCH_NEGATIVE;
} else {
  return self::RESULT_OF_SEARCH_PARTLY;
   }
  }
}

$demo = new Demo();    
print_r($demo->calculateResultOfSearch(["positive","positive"]));

array_count_values() returns an array using the values of the array as keys and their frequency in the array as values.

Here is a simple way to check the values of an array containing the same value using array_count_values function and count if all keys are the same this should equal.

Reference

Sign up to request clarification or add additional context in comments.

2 Comments

I edited OP I forgot to remove the count() but it still doesnt work because array_unique returns an array not a string
@Michael, please try this code, I think this will help you as you are looking.
2

A much simplified version of the code which if array_unique just has 1 value, then return it (also I only call it once rather than repeatedly calling it which is very inefficient)...

private function calculateResultOfSearch(array $imagesResultArray)
{
    $unique = array_unique($imagesResultArray);
    return (count($unique) == 1 ) ? $unique[0]
          : self::RESULT_OF_SEARCH_PARTLY;
}

Edit: I am unfortuantly back after realising I've wasted 20% of the lines I wrote :( If all the items are the same, I can just return the first item of the array passed in so I don't need to store the result of array_unique() at all :-/

private function calculateResultOfSearch(array $imagesResultArray)
{
    return ( count(array_unique($imagesResultArray)) == 1 ) ? 
            $imagesResultArray[0]: RESULT_OF_SEARCH_PARTLY;
}

Comments

0

Try this code :

const RESULT_OF_SEARCH_POSITIVE = "positive";
const RESULT_OF_SEARCH_NEGATIVE = "negative";
const RESULT_OF_SEARCH_PARTLY = "partly";

...

private function calculateResultOfSearch(array $imagesResultArray)
{
    if (!in_array(RESULT_OF_SEARCH_NEGATIVE)) {
        return self::RESULT_OF_SEARCH_POSITIVE;
    } elseif(!in_array(RESULT_OF_SEARCH_POSITIVE)) {
        return self::RESULT_OF_SEARCH_NEGATIVE;
    } else {
        return self::RESULT_OF_SEARCH_PARTLY;
    }
}

1 Comment

This didn't work or you just didn't try it ? (No offense, just because if it failed I want to know it)
0

You're comparing (count(array_unique($imagesResultArray)) which return an int with self::RESULT_OF_SEARCH_POSITIVE or self::RESULT_OF_SEARCH_NEGATIVE which return a string equals to either "positive" or "negative" so it's always false.

You need to change your conditions.

EDIT after OP's edit

PHP: array_unique

Takes an input array and returns a new array without duplicate values.

In your case you might want to check if the resulting array only has one element equals to "positive" or "negative".

$newArray = array_unique($imagesResultArray);

if (count($newArray) == 1 && $newArray[0] === self::RESULT_OF_SEARCH_POSITIVE) {
    return self::RESULT_OF_SEARCH_POSITIVE;
} elseif(count($newArray) == 1 && $newArray[0] === self::RESULT_OF_SEARCH_NEGATIVE) {
    return self::RESULT_OF_SEARCH_NEGATIVE;
} else {
    return self::RESULT_OF_SEARCH_PARTLY;
}

1 Comment

@Michael I just saw it's the same as mine, maybe it's not worth putting a new one ?
0

With your guys answers I came to this "clean" solution.

private function calculateResultOfSearch(array $imagesResultArray)
{
    $results = array_unique($imagesResultArray);

    if(count($results) == 1 && $results[0] === self::RESULT_OF_SEARCH_NEGATIVE) {
        return self::RESULT_OF_SEARCH_NEGATIVE;
    }

    if(count($results) == 1 && $results[0] === self::RESULT_OF_SEARCH_POSITIVE) {
        return self::RESULT_OF_SEARCH_POSITIVE;
    }

    return self::RESULT_OF_SEARCH_PARTLY;
}

3 Comments

Your answer is my latest edit to mine, maybe it's not worth putting a new one ?
@Nesku It's not the same actually
Right I didn't see you removed the else to make the code cleaner, but the actual solution to your problem is the same.

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.