Solved

Array Function To Determine Sum

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
spacing 5 30
advertisement module in core php 4 89
.htaccess file settings 4 36
wordpress issue 2 21
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now