PHP Check if multiple values in array are all positive or all negative or partly
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;
}
}
php
add a comment |
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;
}
}
php
How is count, which returns an integer supposed to be equal to a String with the value of positive or negative?
– maio290
Nov 21 '18 at 9:21
@maio290 my bad I am retarded I think I updated OP
– Michael
Nov 21 '18 at 9:23
Possible duplicate of php How to check if an array of numbers are all positive
– Jean-Marc Zimmer
Nov 21 '18 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
– Michael
Nov 21 '18 at 9:26
Oh, OK, sorry. I though you were talking of positive and negative values ! My bad.
– Jean-Marc Zimmer
Nov 21 '18 at 9:26
add a comment |
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;
}
}
php
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;
}
}
php
php
edited Nov 21 '18 at 9:36
Gufran Hasan
3,56741426
3,56741426
asked Nov 21 '18 at 9:15
MichaelMichael
15910
15910
How is count, which returns an integer supposed to be equal to a String with the value of positive or negative?
– maio290
Nov 21 '18 at 9:21
@maio290 my bad I am retarded I think I updated OP
– Michael
Nov 21 '18 at 9:23
Possible duplicate of php How to check if an array of numbers are all positive
– Jean-Marc Zimmer
Nov 21 '18 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
– Michael
Nov 21 '18 at 9:26
Oh, OK, sorry. I though you were talking of positive and negative values ! My bad.
– Jean-Marc Zimmer
Nov 21 '18 at 9:26
add a comment |
How is count, which returns an integer supposed to be equal to a String with the value of positive or negative?
– maio290
Nov 21 '18 at 9:21
@maio290 my bad I am retarded I think I updated OP
– Michael
Nov 21 '18 at 9:23
Possible duplicate of php How to check if an array of numbers are all positive
– Jean-Marc Zimmer
Nov 21 '18 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
– Michael
Nov 21 '18 at 9:26
Oh, OK, sorry. I though you were talking of positive and negative values ! My bad.
– Jean-Marc Zimmer
Nov 21 '18 at 9:26
How is count, which returns an integer supposed to be equal to a String with the value of positive or negative?
– maio290
Nov 21 '18 at 9:21
How is count, which returns an integer supposed to be equal to a String with the value of positive or negative?
– maio290
Nov 21 '18 at 9:21
@maio290 my bad I am retarded I think I updated OP
– Michael
Nov 21 '18 at 9:23
@maio290 my bad I am retarded I think I updated OP
– Michael
Nov 21 '18 at 9:23
Possible duplicate of php How to check if an array of numbers are all positive
– Jean-Marc Zimmer
Nov 21 '18 at 9:25
Possible duplicate of php How to check if an array of numbers are all positive
– Jean-Marc Zimmer
Nov 21 '18 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
– Michael
Nov 21 '18 at 9:26
@Jean-MarcZimmer no it's not a duplicate, my question has nothing to do with numbers and there are 3 conditions not 1
– Michael
Nov 21 '18 at 9:26
Oh, OK, sorry. I though you were talking of positive and negative values ! My bad.
– Jean-Marc Zimmer
Nov 21 '18 at 9:26
Oh, OK, sorry. I though you were talking of positive and negative values ! My bad.
– Jean-Marc Zimmer
Nov 21 '18 at 9:26
add a comment |
5 Answers
5
active
oldest
votes
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;
}
Sexy! I like it :)
– Michael
Nov 21 '18 at 9:46
There's a missing parenthesis after the ternary.
– Jean-Marc Zimmer
Nov 21 '18 at 10:36
add a comment |
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
I edited OP I forgot to remove the count() but it still doesnt work because array_unique returns an array not a string
– Michael
Nov 21 '18 at 9:24
@Michael, please try this code, I think this will help you as you are looking.
– Gufran Hasan
Nov 21 '18 at 9:32
add a comment |
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;
}
}
checkout my answer, thanks for your help :)
– Michael
Nov 21 '18 at 9:41
This didn't work or you just didn't try it ? (No offense, just because if it failed I want to know it)
– Jean-Marc Zimmer
Nov 21 '18 at 9:45
add a comment |
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;
}
checkout my answers, thanks for your help :)
– Michael
Nov 21 '18 at 9:41
@Michael I just saw it's the same as mine, maybe it's not worth putting a new one ?
– Nesku
Nov 21 '18 at 9:45
add a comment |
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;
}
Your answer is my latest edit to mine, maybe it's not worth putting a new one ?
– Nesku
Nov 21 '18 at 9:52
@Nesku It's not the same actually
– Michael
Nov 21 '18 at 10:14
Right I didn't see you removed theelse
to make the code cleaner, but the actual solution to your problem is the same.
– Nesku
Nov 21 '18 at 10:26
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408671%2fphp-check-if-multiple-values-in-array-are-all-positive-or-all-negative-or-partly%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
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;
}
Sexy! I like it :)
– Michael
Nov 21 '18 at 9:46
There's a missing parenthesis after the ternary.
– Jean-Marc Zimmer
Nov 21 '18 at 10:36
add a comment |
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;
}
Sexy! I like it :)
– Michael
Nov 21 '18 at 9:46
There's a missing parenthesis after the ternary.
– Jean-Marc Zimmer
Nov 21 '18 at 10:36
add a comment |
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;
}
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;
}
edited Nov 21 '18 at 11:39
answered Nov 21 '18 at 9:36
Nigel RenNigel Ren
27.2k61833
27.2k61833
Sexy! I like it :)
– Michael
Nov 21 '18 at 9:46
There's a missing parenthesis after the ternary.
– Jean-Marc Zimmer
Nov 21 '18 at 10:36
add a comment |
Sexy! I like it :)
– Michael
Nov 21 '18 at 9:46
There's a missing parenthesis after the ternary.
– Jean-Marc Zimmer
Nov 21 '18 at 10:36
Sexy! I like it :)
– Michael
Nov 21 '18 at 9:46
Sexy! I like it :)
– Michael
Nov 21 '18 at 9:46
There's a missing parenthesis after the ternary.
– Jean-Marc Zimmer
Nov 21 '18 at 10:36
There's a missing parenthesis after the ternary.
– Jean-Marc Zimmer
Nov 21 '18 at 10:36
add a comment |
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
I edited OP I forgot to remove the count() but it still doesnt work because array_unique returns an array not a string
– Michael
Nov 21 '18 at 9:24
@Michael, please try this code, I think this will help you as you are looking.
– Gufran Hasan
Nov 21 '18 at 9:32
add a comment |
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
I edited OP I forgot to remove the count() but it still doesnt work because array_unique returns an array not a string
– Michael
Nov 21 '18 at 9:24
@Michael, please try this code, I think this will help you as you are looking.
– Gufran Hasan
Nov 21 '18 at 9:32
add a comment |
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
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
edited Nov 21 '18 at 9:47
answered Nov 21 '18 at 9:22
Gufran HasanGufran Hasan
3,56741426
3,56741426
I edited OP I forgot to remove the count() but it still doesnt work because array_unique returns an array not a string
– Michael
Nov 21 '18 at 9:24
@Michael, please try this code, I think this will help you as you are looking.
– Gufran Hasan
Nov 21 '18 at 9:32
add a comment |
I edited OP I forgot to remove the count() but it still doesnt work because array_unique returns an array not a string
– Michael
Nov 21 '18 at 9:24
@Michael, please try this code, I think this will help you as you are looking.
– Gufran Hasan
Nov 21 '18 at 9:32
I edited OP I forgot to remove the count() but it still doesnt work because array_unique returns an array not a string
– Michael
Nov 21 '18 at 9:24
I edited OP I forgot to remove the count() but it still doesnt work because array_unique returns an array not a string
– Michael
Nov 21 '18 at 9:24
@Michael, please try this code, I think this will help you as you are looking.
– Gufran Hasan
Nov 21 '18 at 9:32
@Michael, please try this code, I think this will help you as you are looking.
– Gufran Hasan
Nov 21 '18 at 9:32
add a comment |
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;
}
}
checkout my answer, thanks for your help :)
– Michael
Nov 21 '18 at 9:41
This didn't work or you just didn't try it ? (No offense, just because if it failed I want to know it)
– Jean-Marc Zimmer
Nov 21 '18 at 9:45
add a comment |
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;
}
}
checkout my answer, thanks for your help :)
– Michael
Nov 21 '18 at 9:41
This didn't work or you just didn't try it ? (No offense, just because if it failed I want to know it)
– Jean-Marc Zimmer
Nov 21 '18 at 9:45
add a comment |
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;
}
}
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;
}
}
answered Nov 21 '18 at 9:31
Jean-Marc ZimmerJean-Marc Zimmer
37414
37414
checkout my answer, thanks for your help :)
– Michael
Nov 21 '18 at 9:41
This didn't work or you just didn't try it ? (No offense, just because if it failed I want to know it)
– Jean-Marc Zimmer
Nov 21 '18 at 9:45
add a comment |
checkout my answer, thanks for your help :)
– Michael
Nov 21 '18 at 9:41
This didn't work or you just didn't try it ? (No offense, just because if it failed I want to know it)
– Jean-Marc Zimmer
Nov 21 '18 at 9:45
checkout my answer, thanks for your help :)
– Michael
Nov 21 '18 at 9:41
checkout my answer, thanks for your help :)
– Michael
Nov 21 '18 at 9:41
This didn't work or you just didn't try it ? (No offense, just because if it failed I want to know it)
– Jean-Marc Zimmer
Nov 21 '18 at 9:45
This didn't work or you just didn't try it ? (No offense, just because if it failed I want to know it)
– Jean-Marc Zimmer
Nov 21 '18 at 9:45
add a comment |
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;
}
checkout my answers, thanks for your help :)
– Michael
Nov 21 '18 at 9:41
@Michael I just saw it's the same as mine, maybe it's not worth putting a new one ?
– Nesku
Nov 21 '18 at 9:45
add a comment |
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;
}
checkout my answers, thanks for your help :)
– Michael
Nov 21 '18 at 9:41
@Michael I just saw it's the same as mine, maybe it's not worth putting a new one ?
– Nesku
Nov 21 '18 at 9:45
add a comment |
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;
}
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;
}
edited Nov 21 '18 at 9:34
answered Nov 21 '18 at 9:22
NeskuNesku
4231311
4231311
checkout my answers, thanks for your help :)
– Michael
Nov 21 '18 at 9:41
@Michael I just saw it's the same as mine, maybe it's not worth putting a new one ?
– Nesku
Nov 21 '18 at 9:45
add a comment |
checkout my answers, thanks for your help :)
– Michael
Nov 21 '18 at 9:41
@Michael I just saw it's the same as mine, maybe it's not worth putting a new one ?
– Nesku
Nov 21 '18 at 9:45
checkout my answers, thanks for your help :)
– Michael
Nov 21 '18 at 9:41
checkout my answers, thanks for your help :)
– Michael
Nov 21 '18 at 9:41
@Michael I just saw it's the same as mine, maybe it's not worth putting a new one ?
– Nesku
Nov 21 '18 at 9:45
@Michael I just saw it's the same as mine, maybe it's not worth putting a new one ?
– Nesku
Nov 21 '18 at 9:45
add a comment |
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;
}
Your answer is my latest edit to mine, maybe it's not worth putting a new one ?
– Nesku
Nov 21 '18 at 9:52
@Nesku It's not the same actually
– Michael
Nov 21 '18 at 10:14
Right I didn't see you removed theelse
to make the code cleaner, but the actual solution to your problem is the same.
– Nesku
Nov 21 '18 at 10:26
add a comment |
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;
}
Your answer is my latest edit to mine, maybe it's not worth putting a new one ?
– Nesku
Nov 21 '18 at 9:52
@Nesku It's not the same actually
– Michael
Nov 21 '18 at 10:14
Right I didn't see you removed theelse
to make the code cleaner, but the actual solution to your problem is the same.
– Nesku
Nov 21 '18 at 10:26
add a comment |
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;
}
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;
}
answered Nov 21 '18 at 9:41
MichaelMichael
15910
15910
Your answer is my latest edit to mine, maybe it's not worth putting a new one ?
– Nesku
Nov 21 '18 at 9:52
@Nesku It's not the same actually
– Michael
Nov 21 '18 at 10:14
Right I didn't see you removed theelse
to make the code cleaner, but the actual solution to your problem is the same.
– Nesku
Nov 21 '18 at 10:26
add a comment |
Your answer is my latest edit to mine, maybe it's not worth putting a new one ?
– Nesku
Nov 21 '18 at 9:52
@Nesku It's not the same actually
– Michael
Nov 21 '18 at 10:14
Right I didn't see you removed theelse
to make the code cleaner, but the actual solution to your problem is the same.
– Nesku
Nov 21 '18 at 10:26
Your answer is my latest edit to mine, maybe it's not worth putting a new one ?
– Nesku
Nov 21 '18 at 9:52
Your answer is my latest edit to mine, maybe it's not worth putting a new one ?
– Nesku
Nov 21 '18 at 9:52
@Nesku It's not the same actually
– Michael
Nov 21 '18 at 10:14
@Nesku It's not the same actually
– Michael
Nov 21 '18 at 10:14
Right I didn't see you removed the
else
to make the code cleaner, but the actual solution to your problem is the same.– Nesku
Nov 21 '18 at 10:26
Right I didn't see you removed the
else
to make the code cleaner, but the actual solution to your problem is the same.– Nesku
Nov 21 '18 at 10:26
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408671%2fphp-check-if-multiple-values-in-array-are-all-positive-or-all-negative-or-partly%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
How is count, which returns an integer supposed to be equal to a String with the value of positive or negative?
– maio290
Nov 21 '18 at 9:21
@maio290 my bad I am retarded I think I updated OP
– Michael
Nov 21 '18 at 9:23
Possible duplicate of php How to check if an array of numbers are all positive
– Jean-Marc Zimmer
Nov 21 '18 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
– Michael
Nov 21 '18 at 9:26
Oh, OK, sorry. I though you were talking of positive and negative values ! My bad.
– Jean-Marc Zimmer
Nov 21 '18 at 9:26