Solved

Array Function To Determine Sum

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

690 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