[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

PHP array: Ranking items based on number of duplicates

Posted on 2008-06-20
3
Medium Priority
?
833 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Looking for a new Web Host?

Lunarpages' assortment of hosting products and solutions ensure a perfect fit for anyone looking to get their vision or products to market. Our award winning customer support and 30-day money back guarantee show the pride we take in being the industry's premier MSP.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

649 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