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 6735629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:02:07+00:00 2026-05-26T11:02:07+00:00

I have this result: Array ( [0] => 0, [1] => 2 ) Array

  • 0

I have this result:

Array ( [0] => 0, [1] => 2 ) 
Array ( [0] => 3, [1] => 3 ) 
Array ( [0] => 0, [1] => 1 ) 
Array ( [0] => 3, [1] => 1 ) 
Array ( [0] => 3, [1] => 3 ) 

and it comes from here:

 $total = array(); $i=0; $count = 0;
  foreach($dates as $d){
       $total[$i] = count($value[$d]);
       $i++;
  }
print_r ($total); echo '<br>';

What I want to do is add $total[0] like 0+3+0+3+3 = 9 and add $total[1] like 2+3+1+1+3 = 10.

I only want back the 9 and the 10

Any ideas?

Thanks

  • 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-26T11:02:08+00:00Added an answer on May 26, 2026 at 11:02 am

    If I am correctly understanding the question, you have no need for additional arrays inside the loop. You simply need to accumulate each array value into a different variable:

    $total_1 = 0;
    $total_2 = 0;
    foreach ($array as $a) {
      $total_1 += $a[0];
      $total_2 += $a[1];
    }
    
    echo "Total1: $total_1, Total2: $total_2";
    

    If you have an unknown number of elements, use an array to sum them up by their keys. You need an internal iterator as well. This is, incidentally a pretty common job interview question in my experience.

    // Array to hold all the sums
    $sums = array();
    foreach ($array as $a) {
      // internal iteration
      foreach ($a as $key=>$val) {
        // Add to the key if it already exists, otherwise initialize it to the current value.
        $sums[$key] = isset($sums[$key]) ? $sums[$key] += $val : $val;
      }
    }
    
    // Output your results:
    
    foreach ($sums as $key=>$s) {
      echo "Sum of $key is $s\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a result array that comes back from CodeIgniter looking like this: Array
I have this simple php script <?php echo '<pre>'; // Outputs all the result
I have a table in mysql like this Name Result T1_09_03_2010 fail T2_09_03_2010 pass
I have a xml string: <Test> Result : this is the result</Test> How do
I basically have something like this: void Foo(Type ty) { var result = serializer.Deserialize<ty>(inputContent);
I have a SQL Server 2005 output like this: Date | Result | Sum
I have an xml file like this: <result> <customer> <id>1</id> <name>A</name> </customer> <customer> <id>2</id>
I have a testdata like this: DROP TABLE SELECT_PASS; CREATE TABLE SELECT_PASS(ID INT(20),TESTCASE VARCHAR(20),RESULT
let's say I have a matrix (array) like this example, but much larger: 0
I have this data coming from a REST method using jquery's getJSON method. [Date.UTC(2010,0,0,0,0,0,0),

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.