Solved

PHP array: Ranking items based on number of duplicates

Posted on 2008-06-20
3
820 Views
Last Modified: 2012-06-22
I want to find a more efficient way to build an associated array of strings that's arranged by how many duplicates are found within said array. For example if I have a list of words:
Apple, Peach, Peach, Lemon, Lemon, Orange, Orange, Orange

it will output like:
Orange (3)
Lemon (2)
Peach (2)
Apple (1)

Attached is the solution I came up with, but it's sloppy and really inefficient.
for ($i = 0; $i < count($fruits); ++ $i)

   {

      $count = 0;

      for ($y = 0; $y < count($fruits); ++ $y)

      {

         if($fruits[$i] == $fruits[$y])

         {

            $count++;

         }

      }

      $results[$fruits[$i]] = $count;

      asort($results, SORT_NUMERIC);

      $results = array_reverse($results);

   }
 

   foreach ($results as $key => $value)

   {

      if($value > 1)

         echo '(' . $value.') '.$key .'<br />';

   }

Open in new window

0
Comment
Question by:troubledFish
  • 2
3 Comments
 
LVL 10

Expert Comment

by:wildzero
ID: 21835999
How about eithe of these two
$results = array();

$number_fruits = count($fruits);

for ($i = 0; $i < $number_fruits; ++ $i)

{

  $fruit = $fruits[$i];

  If (isset($results[$fruit])) {

    $count = $results[$fruit]++

  } else {

    $count = 1;  

  }      

  $results[$fruit] = $count;

}
 

asort($results, SORT_NUMERIC);

$results = array_reverse($results);

  

foreach ($results as $key => $value)

{

  if($value > 1)

    echo '(' . $value.') '.$key .'<br />';

}
 
 

or based on your first one. Just move the sort OUT of the loop.
 

for ($i = 0; $i < count($fruits); ++ $i)

   {

      $count = 0;

      for ($y = 0; $y < count($fruits); ++ $y)

      {

         if($fruits[$i] == $fruits[$y])

         {

            $count++;

         }

      }

      $results[$fruits[$i]] = $count;

   }

  asort($results, SORT_NUMERIC);

  $results = array_reverse($results);
 

   foreach ($results as $key => $value)

   {

      if($value > 1)

         echo '(' . $value.') '.$key .'<br />';

   }

Open in new window

0
 
LVL 10

Expert Comment

by:wildzero
ID: 21836002
Actually, just use the first of my ones. That second one (and your original one) is just a bit crazy.
0
 
LVL 16

Accepted Solution

by:
DrNikon224 earned 500 total points
ID: 21836007
array_count_values() - http://www.php.net/array_count_values
arsort() - http://www.php.net/arsort

The function array_count_values() gives you an array with the count of each value stored within, and arsort() will sort the array by value in reverse.
<?

$arr = array("Apple","Peach","Peach","Lemon","Lemon","Orange","Orange","Orange");
 

$repeats = array_count_values($arr);
 

arsort($repeats);

?>

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now