note 104222 added to function.array-sum

From: Date: Wed, 01 Jun 2011 19:12:33 +0000
Subject: note 104222 added to function.array-sum
Groups: php.notes 
Request: Send a blank email to php-notes+get-180176@lists.php.net to get a copy of this message
There was a case where I had a complex array, multi-dimesional and non-symmetric (not all elements of a sub-array were arrays themselves) Although all the leaf elements of this array were numbers. In case you have two variables like that and you wanted to add all the values of the right variable to the left then you would need a function like below. This is a recursive function which adds the values of two multidimensional arrays with the same key structure: <?php function multiDimArrayAdd(& $left, $right) //created by George Pligor { if(is_array($left) && is_array($right)) { foreach($left as $key => $val) { if( is_array($val) ) { multiDimArrayAdd($left[$key], $right[$key]); } $left[$key] += $right[$key]; } } } ?> ---- Server IP: 85.196.44.199 Probable Submitter: 94.71.44.82 ---- Manual Page -- http://www.php.net/manual/en/function.array-sum.php Edit -- https://master.php.net/note/edit/104222 Del: integrated -- https://master.php.net/note/delete/104222/integrated Del: useless -- https://master.php.net/note/delete/104222/useless Del: bad code -- https://master.php.net/note/delete/104222/bad+code Del: spam -- https://master.php.net/note/delete/104222/spam Del: non-english -- https://master.php.net/note/delete/104222/non-english Del: in docs -- https://master.php.net/note/delete/104222/in+docs Del: other reasons-- https://master.php.net/note/delete/104222 Reject -- https://master.php.net/note/reject/104222 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#180176) next »