?
Solved

How do you sum up an array?

Posted on 2011-02-15
5
Medium Priority
?
344 Views
Last Modified: 2012-05-11
I have an array that I would like to sum up i.e. I have the array $price[$j][4] and I would like all the results of that array added together and have a total. I hope that makes sense.
0
Comment
Question by:anotherdotcommer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 34899326
array_sum() maybe?  Please post a small test data set and I will show you how I would do it.
0
 

Author Comment

by:anotherdotcommer
ID: 34899381
Thanks Ray! Here is what I was trying. The page wouldn't load though.... <?php for ($j=0; $j<($price); $j++) {

echo ''.array_sum($price[$j][4]).''; }?>
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 34899408
Maybe something like this would work well for you.
<?php // RAY_temp_another.php
error_reporting(E_ALL);
echo "<pre>";

// TEST DATA
$arr[0][4] = 1;
$arr[1][4] = 2;
$arr[2][4] = 3;
$arr[3][4] = 4;

// ACTIVATE THIS TO SEE THE TEST DATA
// var_dump($arr);

// ADD IT ALL UP
$sum = 0;
$num = 0;
$max = count($arr);
while ($num < $max)
{
    $sum = $sum + $arr[$num][4];
    $num++;
}
echo $sum;

Open in new window

0
 

Author Comment

by:anotherdotcommer
ID: 34899510
It worked!! Thanks
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 34899520
Glad to help!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question