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

  • SEARCH
  • Home
  • 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 3434088
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:39:19+00:00 2026-05-18T07:39:19+00:00

I have 2 arrays in PHP that look something like this: $rows = array(11,12,14,14,11,13,12,11);

  • 0

I have 2 arrays in PHP that look something like this:

$rows = array(11,12,14,14,11,13,12,11);
$cols = array(1,2,1,2,2,2,1,1);

I need to combine these arrays in a way that tells how many of each $cols value is in each $rows value.

So my result should look something like this:

Array
(
    [0] => Array
        (
            [row] => 11
            [1] => 2 //the count of 1 cols for 11
            [2] => 1 //the count of 2 cols for 11
        )

    [1] => Array
        (
            [row] => 12
            [1] => 1
            [2] => 1
        )

    ...
)

The values of $rows and $cols will change based upon the users input, they may even be strings.

Clarification:

The duplicates values come from the data. Think survey results or test questions. So question 11 had 2 people answer 1 and 1 person answer 2.

Question:

How do I count the number of occurrences of $cols in $rows and add the results into a multidimensional array?

  • 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-05-18T07:39:20+00:00Added an answer on May 18, 2026 at 7:39 am

    I figured it out. It was not as complicated as I first thought. Just needed a function to calculate the sum. Perhaps it could be more efficient, I would love any ideas on that, but here it is:

    $rowColumns = array();
    
                for($i=0;$i<=$count-1;$i++)
                {
                    $currentRow = $rows[$i];
                    $currentCol = $cols[$i];
    
                    //count occurences of columns in row
                    $colSum = $this->getColumnOccurences($count,$rows,$cols,$currentRow,$currentCol);
    
                    $rowColumns[$currentRow][$currentCol] = $colSum;
                }
    
    private function getColumnOccurences($count,$rows,$cols,$rowValue,$colValue)
        {
            $retValue = 0;
    
            for($i=0;$i<=$count-1;$i++)
            {
                if($rows[$i] == $rowValue && $cols[$i] == $colValue)
                {
                    $retValue = $retValue + 1;
                }   
            }
    
            return $retValue;
        }
    

    The result is:

    Array ( 
            [11] => Array ( [1] => 2 [2] => 1 ) 
            [12] => Array ( [1] => 1 [2] => 1 ) 
            [13] => Array ( [2] => 1 ) 
            [14] => Array ( [1] => 1 [2] => 1 ) 
          )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In PHP I have an array that looks like this: $array[0]['width'] = '100'; $array[0]['height']
I have a url that look like this reg.php?lang=no_NO&passkey=test and im trying to get
I have a php array that has a bunch of data that I need
In PHP I can name my array indices so that I may have something
I have a PHP function that returns something: function myfunction() { $array = array('one',
I realized that I can have problems with single quotes in php arrays: <?php
I have a php file somejson.php that echos a json encoded array {jsonone:first json,jsontwo:second
I have a PHP class that stores a complex multidimensional array, and rather than
I am using PHP and I have an array of user images that I
What is the PHP preg_replace in C#? I have an array of string that

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.