1

i have an array with many values and i want to get one value is many show of all values. Like this.

my array

$allValues = array(0,1,1);         // i want to get 1, because two 1 vs one 0

// other example
$allValue = array(0,0,0,1,1);      // I want to get 0, because three 0 vs two 1

// other example
$allValues = array(0,1);           // I want to get 0, because one 0 vs one 1 is 50:50 but 0 is first value

Sorry for my bad english.

2

5 Answers 5

3
<?php
$allValues = array(0,1,1);   

$result=array_count_values($allValues);   // Count occurrences of everything
arsort($result);                          // Sort descending order
echo key($result);                        // Pick up the value with highest number
?>

Edit: I've used key() because you are interested in knowing the value which has most number of occurrences and not the number itself. If you just need the number you can remove key() call.

Fiddle

8
  • The shortest and efficient solution. Thumbs up! Commented Oct 13, 2014 at 5:16
  • I've a feeling this isn't the shortest. Think it can be shortened further :) Commented Oct 13, 2014 at 5:17
  • Nice man, thanks very much @Hanky �? Panky. your answer vote up Commented Oct 13, 2014 at 6:23
  • How about the third example? This returns 1 for array(0, 1), not 0 as the OP requested. Otherwise, an ingenious solution.
    – Antti29
    Commented Oct 13, 2014 at 6:43
  • I could go ahead and code for that too but you know OP should take the idea and then build upon it for themselves :) Otherwise it becomes a copy-paste job and no learning is inovlved Commented Oct 13, 2014 at 6:59
3

try this

$allValues = array(0,0,0,1,1); 
$count = array_count_values($allValues);
echo $val = array_search(max($count), $count);
5
  • 1
    This solution works with all inputs and uses less code than the accepted answer. Well done! You should, however, break down what you did here, it might be immediately obvious to everyone.
    – Antti29
    Commented Oct 13, 2014 at 7:17
  • @Antti29 Yes this code works will all the given inputs but it does not use less code than the accepted answer. Accepted answer uses 3 function calls and one assignment, this answer uses 3 function calls and 2 assignments. Just because some function calls are nested, doesn't that becomes less code. Commented Oct 13, 2014 at 8:58
  • @Hanky�?Panky And I suppose sorting an array (especially if it gets very large) is more efficient than calling array_search() and max()? You are using one less assignment because you're only echoing the result, which is something this answer could do just as well.
    – Antti29
    Commented Oct 13, 2014 at 9:18
  • Yep, do a benchmark and see which answer is faster if you like those numbers. I set up one for you but then i deleted because thats not the question. You can benchmark both answers to see. Commented Oct 13, 2014 at 9:36
  • I did just that, and found the results to be, for all intents and purposes, identical (the differences varied and were within the margin of error). Would your code still be as fast if you did get it to return 0 with the third example input?
    – Antti29
    Commented Oct 13, 2014 at 10:00
0

Works only with 0s and 1s

function evaluateArray($array) {
    $zeros = 0;
    $ones  = 0;

    foreach($array as $item) {
        if($item == 0) {
            $zeros++;
        } else {
            $ones++;
        }
    }
    // Change this if you want to return 1 if the result is equal
    // To return ($ones >= $zeros) ? 1 : 0; 
    return ($zeros >= $ones) ? 0 : 1;
}
0

Try this:

function find_value($array) {
    $zeros = 0;
    $ones = 0;
    for($i = 0; $i < count($array); $i++) {
        ($array[$i] == 0) ? $zeros++ : $ones++;
    }
    if($zeros == $ones) return $array[0];
    return ($zeros > $ones) ? 0 : 1;
}
0

you can use array_count_values — Counts all the values of an array

<?php
$array = array(1, "hello", 1, "world", "hello");
print_r(array_count_values($array));
?>

Output

Array ( [1] => 2 [hello] => 2 [world] => 1 )

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.