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.

Ecko Santoso
  • 500
  • 4
  • 10

5 Answers5

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

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
  • The shortest and efficient solution. Thumbs up! – Hendry Tanaka Oct 13 '14 at 05:16
  • I've a feeling this isn't the shortest. Think it can be shortened further :) – Hanky Panky Oct 13 '14 at 05:17
  • Nice man, thanks very much @Hanky 웃 Panky. your answer vote up – Ecko Santoso Oct 13 '14 at 06:23
  • How about the third example? This returns `1` for `array(0, 1)`, not `0` as the OP requested. Otherwise, an ingenious solution. – Antti29 Oct 13 '14 at 06: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 – Hanky Panky Oct 13 '14 at 06:59
  • I do agree with that, however this is still an incomplete answer and one rather hastily accepted (which, of course, is the OP's fault). – Antti29 Oct 13 '14 at 07:15
  • That's fine I respect your opinion and won't mind if the OP undo's the accept based on that. – Hanky Panky Oct 13 '14 at 07:20
  • I'm sory, my question is less complete. the values of array will not contains just 0 and 1, i will use that for value of string like this. $allValues = array('Oke','Not Oke','Not Oke','Maybe','Oke','Oke'); and i want the result is Oke, why Oke? because Oke is most populations of value. This is my reason for acceptable this answer. – Ecko Santoso Oct 13 '14 at 08:35
3

try this

$allValues = array(0,0,0,1,1); 
$count = array_count_values($allValues);
echo $val = array_search(max($count), $count);
noufalcep
  • 3,446
  • 15
  • 33
  • 51
  • 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 Oct 13 '14 at 07: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. – Hanky Panky Oct 13 '14 at 08: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 Oct 13 '14 at 09: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. – Hanky Panky Oct 13 '14 at 09: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 Oct 13 '14 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;
}
napolux
  • 15,574
  • 9
  • 51
  • 70
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;
}
Yavor
  • 671
  • 3
  • 9
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 )

Nitin Kaushal
  • 211
  • 1
  • 7