Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7956165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:54:51+00:00 2026-06-04T03:54:51+00:00

We have an array filled with category names (many of them repeats) and we

  • 0

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];
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T03:54:52+00:00Added an answer on June 4, 2026 at 3:54 am

    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);
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one array that is filled by mysql_query. Values that are in this
I have two arrays, @names and @employees , that are filled with strings that
so I have an Array that is filled with a bunch of repeats like
I have an array filled with instances of a custom class which contains two
Simplified, I have to solve the following problem: You have a 2-dimensional array filled
I have an array which is filled with platforms that are supposed to move.
I have a 2 dimensional array filled with 0s and 1s. I have to
If I have an array in Javascript that has been filled sporadically, how do
I have an array of dictionaries. Each dictionary has one category. I want to
I have an array that is filled with values dynamically and I have to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.