Solved

Array Function To Determine Sum

Posted on 2009-07-06
3
218 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
[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
  • 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

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…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
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 …

734 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