Jump to content

Counting & Grouping Values in array


mainstreetop

Recommended Posts

I have two arrays... One is brand($a) and the other is brandId($b):

 

$a = array("malibu","impala","escort","camaro","impala","venture","impala", "venture");
$b = array("100","101","102","103","101","104","101", "104");

 

I want to echo the unique occurrence of brand & brandId along the count within each brand.  Something similar to:

 

brand - brandId - count

malibu - 100 - 1

impala - 101 - 3

escort -102 - 1

camarao - 103 - 1

venture - 104 - 2

 

I guess I want a multi-dimensional array containing brand, brandId and count.  I see array_count_values may help, but having trouble seeing it all the way through.

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/207167-counting-grouping-values-in-array/
Share on other sites

Something like this may be what you're looking for:

<?php
$a = array("malibu","impala","escort","camaro","impala","venture","impala", "venture");
$b = array("100","101","102","103","101","104","101", "104");
$brands = array();
for($i=0;$i<count($a);++$i) {
   if (!array_key_exists($a[$i],$brands)) {
      $brands[$a[$i]] = array($b[$i] => 0);
   }
   $brands[$a[$i]][$b[$i]]++;
}
echo '<pre>' . print_r($brands,true) . '</pre>';
?>

 

or

 

<?php
$a = array("malibu","impala","escort","camaro","impala","venture","impala", "venture");
$b = array("100","101","102","103","101","104","101", "104");
$brands = array();
for($i=0;$i<count($a);++$i) {
   if (!array_key_exists($a[$i] . '-' . $b[$i],$brands)) {
      $brands[$a[$i] . '-' . $b[$i]] = 0;
   }
   $brands[$a[$i] . '-' . $b[$i]]++;
}
echo '<pre>' . print_r($brands,true) . '</pre>';
?>

Ken

@kenrbnsn

Thank you for taking the time to reply.  I am going to test your code.  I am pretty new to php so it takes me a while to walk through the steps and understand.

 

I had been making making attempts to solve the  problem.  I came up with what's below. However, your code seems more simplified.

 

$a = array("malibu","impala","escort","camaro","impala","venture","impala", "venture");
$b = array("100","101","102","103","101","104","101", "104");

$a1 = array_count_values($a);  
$a2 = array_unique($a);            
$a3 = array_unique($b);

$count = array_values($a1);
$brand = array_values($a2);
$brandId = array_values($a3);

for($x=0; $x<count($brand); $x++)  {
   echo "<p>" .$brand[$x].  " - " .$brandId[$x].  " - " .$count[$x]. "<br/>";
}

 

Thanks again.

Mike

To get the results out of my samples, use this

<?php
foreach ($brands as $brand => $brandid) {
   foreach ($brandid as $id => $cnt) {
      echo $brand . ' - ' . $id . ' - ' . $cnt . "<br />\n";
   }
}
?>

with the first solution and

<?php
foreach ($brands as $key => $cnt) {
   echo str_replace('-',' - ',$key) . ' - ' . $cnt . "<br />\n";
}
?>

with the second.

 

Remove the

<?php
echo '<pre>' . print_r($brands,true) . '</pre>';
?>

in each case.

 

Ken

@kenrbnsn...

 

I am using your code to get the brand, id and count.  I found that your example was easier for me to sort the brands using ksort() to get the brands to appear alphabetically.  Actuaally, you code was simply easier all around.

 

Would you, by chance, know how to sort the keys in a specific order?  I've looked into uksort(), but the order I'd like to see is two specific brands (if they exist) and then alphabetically for the remaining brands.

 

In my previous example, I'd like to see Malibu and Chevy as always the number 1 & 2 brand respectively (again, if they exist).  The remaining brands can simply sorted by alpha.

 

If you have any thoughts... Thanks. Mike

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.