Solved

Array Function To Determine Sum

Posted on 2009-07-06
3
216 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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

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…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…
The viewer will learn how to dynamically set the form action using jQuery.

803 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