note 85375 deleted from function.array-sum by levim

From: Date: Tue, 18 Mar 2014 19:43:35 +0000
Subject: note 85375 deleted from function.array-sum by levim
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-198967@lists.php.net to get a copy of this message
Note Submitter: superman1101 ---- To elaborate on djyb2003 at yahoo dot fr. His function can be converted to recursive, but i've made a smaller function to do so: <?php function array_sum_all($array) { $func = create_function("&\$arr=0,\$key=0","static \$a=0; if(is_int(\$arr)) { \$a+=\$arr; } return \$a;"); array_walk_recursive($array,$func); return $func(); } $tab = array( 0 => array ("val1" => 2,"val2" => 5), 1 => array ("val1" => 6,"val2" => 10), "toto" => array ("val1" => 15,"val2" => 50) ); var_dump(array_sum_all($tab)); //This will output: int(88); ?> hope this helps someone

« previous php.notes (#198967) next »