954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Getting sum of array

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 '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
                        )

                )

        )
sciulli
Newbie Poster
1 post since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

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.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You