Results 1 to 5 of 5

Thread: array question

  1. #1

    Thread Starter
    Addicted Member Beasts's Avatar
    Join Date
    Oct 2006
    Posts
    147

    array question

    hello,
    i've been wondering how to find the most frequently occurring value in an array? the array is going to be consist of either numbers or strings or mixed.

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: array question

    not sure if this would be the most efficient way, but I wrote a quick function to do what you want. it will return the most recurring value and how many times it recurred in the array. this will not work for multidimensional arrays but it might be easy to make it do so.
    PHP Code:
      function array_mostrecurring($array){
        
    //check if we have an array
        
    if(!is_array($array)) return false;

        
    $c = array();           //counting array
        
    $return['value'] = "";  //highest value (str)
        
    $return['count'] = 0;   //highest value (int)

        
    foreach($array as $value){
          
    $c[$value]++;
          if(
    $c[$value] > $return['count']){
            
    $return['count'] = $c[$value];
            
    $return['value'] = $value;
          }
        }
        return 
    $return;
      } 
    and you could use it like so:
    PHP Code:
    <pre>
    <?php
      $test_array 
    = array(1113345678999999111);
      
    print_r(array_mostrecurring($test_array));
    ?>
    </pre>
    in this example, there are six values of 1 and six values of 9, but because the 9s were all found first -- setting the highest recurring value at 6 -- it will returns 9 as being the most recurring value. it will print the following:
    Code:
    Array
    (
        [value] => 9
        [count] => 6
    )
    which means you can use it like so:
    PHP Code:
    <?php
      $my_array 
    array_mostrecurring($other_array);
      echo 
    "The highest recurring value in $other_array is " $my_array['value'] . "! It recurred " $my_array['count'] . " times.";
    ?>
    let me know if that works for you.

    edit: oh, and this will work fine with both strings and integers. integers were just easier to use for an example.
    Last edited by kows; Jan 16th, 2009 at 10:04 PM.

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: array question

    Here's a shorter alternative.

    PHP Code:
    function array_mode($v)
    {
      
    $z array_count_values($v);
      
    arsort($z);
      
    $k array_keys($z);
      return 
    $k[0];

    Last edited by penagate; Jan 17th, 2009 at 11:06 PM.

  4. #4

    Thread Starter
    Addicted Member Beasts's Avatar
    Join Date
    Oct 2006
    Posts
    147

    Re: array question

    thank you both very much. i like penagate's short function. could please explain the algorithm to me?

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: array question

    array_count_values counts the frequency of every unique value in the input array and produces an output array composed of the values as keys and their frequencies as values. arsort sorts the array by value (frequency, in this case) in reverse, while preserving key/value associativity; this gives us the modal value as the first element of the array. array_keys produces an array whose values are the keys of the input array; this is so that we can return the value rather than its frequency.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width