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];
array_uniquecan remove duplicates.