Solved

Array Function To Determine Sum

Posted on 2009-07-06
3
217 Views
Last Modified: 2012-05-07
I am looking for a PHP function, where I can pass in an array, and a key name, and it will return a sum for all those keys in the array....Ideally it would work with multi-dimensional arrays.
0
Comment
Question by:interclubs
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
BrianMM earned 500 total points
ID: 24785413
try:

array_sum_key( $arr, "keyname")

Should work on MD arrays.
function array_sum_key( $arr, $index = null ){
    if(!is_array( $arr ) || sizeof( $arr ) < 1){
        return 0;
    }
    $ret = 0;
    foreach( $arr as $id => $data ){
        if( isset( $index )  ){
            $ret += (isset( $data[$index] )) ? $data[$index] : 0;
        }else{
            $ret += $data;
        }
    }
    return $ret;
}

Open in new window

0
 
LVL 11

Expert Comment

by:BrianMM
ID: 24785470
FYI result:

a = array
  'x1' =>
    array
      'x' => int 2
      0 => int 3
      1 => int 4
  'x2' =>
    array
      'x' => int 4
      0 => int 3
      1 => int 4
  'x3' =>
    array
      'x' => int 2
      0 => int 3
      1 => int 4
  'x4' =>
    array
      'x' => int 10
      0 => int 3
      1 => int 4

array_sum_key( $a, "x") = 18
0
 
LVL 6

Expert Comment

by:gokce_yalcin
ID: 24793408
Just a little enhancement for recursive sum with no points to deserve.

So it can support group sums like

$a=array('x2'=>1,2,3,'x'=>array(1,2,3,4),'x2'=>1,2,3,'x'=>array(1,2,3,4))

function array_sum_key( $arr, $index = false ){
    if(!is_array( $arr ) || sizeof( $arr ) < 1){
        return 0;
    }
    $ret = 0;
    foreach( $arr as $id => $data ){
        if (is_array($data)) $ret += array_sum_key($data,$index);
        else {
        if( $index ){
            $ret += (isset( $data[$index] )) ? $data[$index] : 0;
        } else {
            $ret += $data;
        }
        }
    }
    return $ret;
}

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delete  php pages of a part of our site 8 34
ajax to record click 3 16
Log in through ID 5 19
Showing multiple maps using PHP and Google Maps 8 21
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…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

821 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