php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #23964 Multi-dimensional array sum
Submitted: 2003-06-02 15:55 UTC Modified: 2003-06-21 14:59 UTC
Votes:4
Avg. Score:2.5 ± 1.7
Reproduced:3 of 4 (75.0%)
Same Version:2 (66.7%)
Same OS:1 (33.3%)
From: prof_moriarty at veryfast dot biz Assigned:
Status: Wont fix Package: Feature/Change Request
PHP Version: 4.3.2 OS: Win98 se
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: prof_moriarty at veryfast dot biz
New email:
PHP Version: OS:

 

 [2003-06-02 15:55 UTC] prof_moriarty at veryfast dot biz
This is tricky to explain, but easy to understand.

At present there's a function: array_sum that adds up all the values in an array.

I would like to suggest somthing similar, and at the same time intrinsically different.

The ability to sum up an elements value in a multi-dimensional array.

Example multi-dim-array:

array (
  'box1' =>
  array (
    'height' => 5,
    'length' => 10,
    'width' => 3.5,
  ),
  'box2' =>
  array (
    'height' => 17,
    'length' => 9,
    'width' => 8,
  ),
)


The above array contains two boxes and a few dimensions...

What do i do if i want to work out the combined height of the boxes (to see if they can fit into a space)? I could write a little function to do it sure.
But i figure quite a few other people could use this little function too, so i'll suggest it to you.



some clarification, using the above example array.
the syntax for the function could be:

mixed proposed_function(array input, string element_name)


An example of it in use:

$result = proposed_function($array_to_manipulate, 'height');
echo $result;

The result would be:
22 //becase 5 + 17 = 22 :)


I hope thats clear...

Keep up the good work folks... :)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-06-02 17:07 UTC] pollita@php.net
Perhaps someone else will be motivated enough to implement this (feel free to write it yourself and submit a patch to internals@lists.php.net) but here's some userspace code which accomplishes what you want.

$total = 0;
foreach($array_to_manipulate as $key => $value)
  if ($key == 'height')
    $total += $value;

Which I'm sure you can see is easy enough to wrap in a function.
 [2003-06-02 17:43 UTC] prof_moriarty at veryfast dot biz
sorry, i can't code in C/++, so can't do it myself. :)

however that code can't work (i just tested it to make sure).
You missed a layer of the looping.

It should be:

$total = 0;
foreach($array_to_manipulate as $boxes => $box_arrays){
  foreach($box_arrays as $key=> $value){
    if ($key == 'height') {
      $total += $value;
    }
  }
}

That should work.....
As you can see, with sizeable arrays, it could go around quite a few times... Which (from a performance front) would be better if it was done in c(++).... Hint Hint... ;)
 [2003-06-02 17:47 UTC] pollita@php.net
Ah, touche... good catch...
 [2003-06-02 19:52 UTC] prof_moriarty at veryfast dot biz
You see, if someone with a php.net e-mail addy get's it wrong, what hope do us 'mere-mortals' have?!?!?! hehe
;)
 [2003-06-03 06:36 UTC] mgf@php.net
This should be on php-general by now, but...

Why the inner foreach?  According to the stated problem, you *know* which key you're looking for, so why not go straight for it?  So...

function proposed_function($array, $key)
{
   $total = 0;
   foreach($array as $subarr)
      if (isset($subarr[$key]))
         $total += $subarr[$key];
   return $total;
}

 [2003-06-03 12:37 UTC] prof_moriarty at veryfast dot biz
fair point & good catch.
Mind, now you've gone and re-inforced my point about us 'mere mortals'.
;)

I'm assuming 'php-general is the general mailing list.
 [2003-06-03 22:22 UTC] sniper@php.net
can be done in user-space.. :)

 [2003-06-04 09:54 UTC] prof_moriarty at veryfast dot biz
Yes, but that's not the point.
array_sum can be done in user_space.
as can:
array_fill,
array_pad,
array_pop
array_push
array_rand
array_count_values

Indeed, most of the array_ function can be done with a couple of lines of code.

i.e. array_push:
From the manual!!!!:
"Has the same effect as: 

$array[] = $var;"


That's just ONE line of code, and an apricot could get in it's sleep. :)


Noting that it was only the third attempt that got it even relatively correct, it goes to show that it's not so simple... (i've been playing with PHP for 2+ years, and the other wrong one was a @php.net type!!!)

So could someone _please_ unset this as 'won't fix' (seeing as i can't), and reset to 'open?

Thanx
 [2003-06-04 09:58 UTC] derick@php.net
I also think we shouldn't add the multilevel functionality here, as it's pretty easy to do in userspace (yes, array_sum() should have been something for userspace too). All other functions you mentioned *can* be done in userspace, but would be much slower.
 [2003-06-04 10:15 UTC] prof_moriarty at veryfast dot biz
Unless you hadn't noticed, there isn't really much 'multi-level' functionality in PHP anyway!?!?! (as in the ability to manipulate them easily).
Let's see what we can find:

array_multisort : great function.

that's really about it. Sure numerous other functions can mess with them, but mostly you'd have to put them into a loop to actually get through all the dimensions.
Even the noted 'array_sum' doesn't work with multi-dimensionals (or at least i spotted a post somewhere in this bug collection that stated as much).


When you say "would be much slower", Do you mean from a processing front (several micro seconds), or from a coding front (in which case any proposed function would be quicker than writing it yourself)?
If you mean by time, then i'd only have to guess (noting my lack of knowledge of benchmarking and C/++) that the proposed function would actually gain a speed boost from being written into PHP's core, primarily because it requires a loop (and on each iteration, a little more time would be gained over a PHP implementation).


As you can probably tell, i don't plan on abandoning this little function without a fight, simply because it can be 'user implemented' (which i recognise and appreciate is a criteria for inclusion).
 [2003-06-04 11:35 UTC] derick@php.net
blah blah, write a patch yourself if you want this sooo badly. But then still, the chance of being it included in PHP is sli,.
 [2003-06-04 19:44 UTC] prof_moriarty at veryfast dot biz
you are a Humbug...
:(

I can't code in C, and i'm not planning on adding it to my repertouir any time soon.

Maybe i should say that next time someone else makes a suggestion for my online game. Tell them to write it themselves, and still turn it away...

There goes me ever trying to be helpful with PHP's dev in the future....

There should be more 'pollita's / MGF's and less Dereks in the world...

Ungratefully. :(
Me
 [2003-06-04 21:18 UTC] philip@php.net
Think of it this way.  This is bug #23964, that's a lot of bugs.  I can't speak for anyone else but know that Derick closes many bugs and is constantly working to improve PHP.  Maybe he saw this report and feels there are many more pressing needs than adding another array function, especially one that can be done fairly easy in userspace.  I'm not proposing anything here, but here's a version with some checks:

function array_sum_multi($arr, $key) {
    if (!is_array($arr)) {
        return false;
    }
    $sum = 0;
    foreach ($arr as $info) {
        if (isset($info[$key]) && is_numeric($info[$key])) {
            $sum += $info[$key];
        }
    }
    return $sum;
}

Basically, if I were you, I wouldn't lose too much sleep over it, or take it personally.  Also, you will notice that your request is a duplicate of a preexisting feature request for this same feature, see also:

http://bugs.php.net/bug.php?id=12028

It's the same idea but a different implementation (which one is correct? ;), and it remains open...
 [2003-06-05 09:20 UTC] prof_moriarty at veryfast dot biz
Ah the dilema. :)
On the one hand, i appreciate that Derrick is assisting donating time (as are you all, and as i do for my Open-Source Game), and i realise that developers efforts for such things are rarely cared about. On the other hand, Derricks 'Blah Blah' Response is hardly the way to conduct an informed arguement/debate.

The bug you referenced as 12028 was actually returned by the 'check to see if it's already done' checker... So that works well. :)
It's actually the bug report i was refering to when i said:

"Even the noted 'array_sum' doesn't work with multi-dimensionals (or at least i spotted a post somewhere in this bug collection that stated as much)."

I believe the proposed functionality of that function would be 'array_sum' but for all entries in all layers.

This thread proposes only 'selected' entries, rather than all of them (i've never come across the need to add all entries in a single array, let alone multi-dimensional. But due to the nature of online games, it's often required that I add up the values of a certain element within a multi-dimensional).
So they are 'both' correct IMHO :) .
Though i would suggest that that function be simply an alteration of array_sum to accomodate the extra layers for that suggestion.

But following Dereks logic, if this proposal should be 'won't fix', then why isn't that one?
They are both user-function type things.


But that report does also point to the lack of functionality for multi-dimensional arrays.

Thanx for the prompt responses though. Though I may not show it, I do appreciate the work you folks do...
just try to get Derek to fix more bugs and post less posts... everyone will be happier... ;)
 [2003-06-05 09:29 UTC] derick@php.net
Writing my name correct makes me happy too ;-)
 [2003-06-05 13:48 UTC] prof_moriarty at veryfast dot biz
Sorry. :)
 [2003-06-21 14:59 UTC] derick@php.net
Thanks for your interest in PHP.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 19 04:01:33 2024 UTC