1

We have an array filled with category names (many of them repeats) and we need to build a two dimensional array that eliminates the repeats on one side of the array, and on the other side of the array has the totals of how many times that category name appeared in the original array. Here are two pictures so you can better understand what I am describing: http://postimage.org/image/ptms64cl9/ and http://postimage.org/image/70x6qt0l9/. Now, I am sure there is more than one way to do this, but I want to understand the way the book is doing it. Here is the code, note that $mismatch_categories holds the original array of repeated categories:

 $category_totals = array(array($mismatch_categories[0], 0));
    foreach ($mismatch_categories as $category) {
      if ($category_totals[count($category_totals) - 1][0] != $category) {
        array_push($category_totals, array($category, 1));
      }
      else {
        $category_totals[count($category_totals) - 1][1]++;
      }
    }

One of the main things I do not understand about this example is the array within an array. Arent there actually 3 arrays here:

 $category_totals = array(array($mismatch_categories[0], 0));

If there are 3 arrays, how do I use their indexes? Something like this maybe?:

 $category_totals[0][0][0];
3
  • 7
    Sounds like the book's author hadn't come across array_count_values() - php.net/manual/en/function.array-count-values.php Commented May 18, 2012 at 19:24
  • 1
    + 1 to Mark's response. Also, array_unique can remove duplicates. Commented May 18, 2012 at 19:52
  • +1 for providing the images - much easier for us to understand. Commented May 19, 2012 at 13:27

3 Answers 3

1

Hope it will help you understand.

<?php
     echo '<pre>';
     $mismatch_categories = array('cat', 'cat', 'cow', 'book', 'box', 'box', 'box');
     echo 'Input Mismatch Category::<br />';
     print_r($mismatch_categories);
     echo '<br />';
     $category_totals = array(array($mismatch_categories[0], 0));
     echo 'categroy totals that holds final data' . '<br />';
     $counter = 0;
     print_r($category_totals);
     foreach ($mismatch_categories as $category) {
         echo 'Iteration ' . $counter++ . '<br /><br />';
         echo 'Current category value::' . $category . "<br /><br />";
         echo 'Value of category_totals[' . count($category_totals) . '- 1][0] :: ' .         $category_totals[count($category_totals) - 1][0] . '<br/><br />';
         echo 'Are they equal' . '<br />';
         if ($category_totals[count($category_totals) - 1][0] != $category) {
              echo 'Not matched so pushed into array with occurence of one<br />';
              array_push($category_totals, array($category, 1));
         } else {
              echo 'matches so count is increased by 1' . "<br />";
              $category_totals[count($category_totals) - 1][1]++;
         }
         echo 'category totals:' . '<br />';
         print_r($category_totals);
   }
   echo 'Final value of category_totals::';
   print_r($category_totals);
?>
Sign up to request clarification or add additional context in comments.

1 Comment

You, my friend, are what we call a lifesaver. Thank you!
1

Check array_count_values() function ( http://www.php.net/manual/pl/function.array-count-values.php ) should do the trick

Source array:

array(
    0 => 'Cat 1',
    1 => 'Cat 1',
    2 => 'Cat 1',
    3 => 'Cat 2',
    4 => 'Cat 2',
    5 => 'Cat 3',
    6 => 'Cat 4',
)

Result array_count_values():

array(
    'Cat 1' => 3,
    'Cat 2' => 2,
    'Cat 3' => 1,
    'Cat 4' => 1,
)

2 Comments

array_values() is not the right function for this. I think you meant array_count_values()?
Yes, you're right, I meant array_count_values(), reply corrected
0

To follow up on the suggestions in the comments:

<?php
$mismatch_categories = array('cat', 'cat', 'cow', 'book', 'box', 'box', 'box');

$cat_counts = array_count_values($mismatch_categories);
// = array('cat' => 2, 'cow' => 1, 'book' => 1, 'box' => 3)

$categories = array_unique($mismatch_categories);
// = array('cat', 'cow', 'book', 'box');
?>

In other words, exactly what you're looking for.

The only reason to write your own processing loop here would be for coding practice.

1 Comment

Yes, I cannot thank you enough. The previous example you gave was great coding practice. It showed me exactly how the processing loop was functioning. Good to know there was a much easier solution though! lol

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.