RSS Forums RSS
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 1736 | Replies: 1
Reply
Join Date: Jul 2007
Posts: 1
Reputation: sciulli is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sciulli sciulli is offline Offline
Newbie Poster

Getting sum of array

  #1  
Jul 28th, 2007
Totally lost here. The output of one record in the array is posted below. I would like to get the sum of all the records in field [5] of the [Transporter Log].

I have tried various variations of the following (which I cut/paste from another thread). $subtotal allows returns '0'. I'm thinking I need to do nested loops to reach the [Transporter Log] array but not sure how to accomplish that. Any guidance would be much appreciated.

$item = $datac; //results of mysql query
        foreach($item as $key => $val){
            $data[$key] = explode(",", $val);
        }
        for($k = 1; $k < count($item); $k++){
            $subtotal[$k] = substr($data[$k][4],2);
        }
        foreach($subtotal as $val)
          echo $val.' ';
          
        echo '<br>Newest Sum: '.(array_sum($subtotal));
Here's the output of the first record in the array:
Array
(
    [0] => Array
        (
            [Transporter Log] => Array
                (
                    [10] => Array
                        (
                            [2] => Array
                                (
                                    [0] => 2007-07-10
                                )

                            [4] => Array
                                (
                                    [0] => 1
                                )

                            [5] => Array
                                (
                                    [0] => 1
                                )

                            [6] => Array
                                (
                                    [0] => Shelter Wildlife Delivery
                                )

                            [17] => Array
                                (
                                    [0] => 
                                )

                            [25] => Array
                                (
                                    [0] => 
                                )

                            [uid] => 1
                            [mod_date] => 2007-07-26
                            [proxyid] => 1
                            [creation_date] => 2007-07-26
                            [creator_email] => admin@mydomain.com
                        )

                )

        )
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Posts: 4,832
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 324
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Getting sum of array

  #2  
Jul 29th, 2007
Start of by breaking your problem into smaller chunks.

First find out if it is doing the very first bit, then the second third etc.
... the hat of 'is this a cat in a hat?'
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 6:26 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC