note 92534 deleted from function.array-product by levim

From: Date: Tue, 18 Mar 2014 19:45:37 +0000
Subject: note 92534 deleted from function.array-product by levim
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-198973@lists.php.net to get a copy of this message
Note Submitter: bishop ---- gmail @ algofoogle is right, so we can extend our own array_product() to flexibly accept the empty product value. Zero (0) is the default (to be compatible with PHP behavior), but you could change this to 1 for mathematical purposes or null for logical. <?php if (! function_exists('array_product')) { function array_product($array, $emptyProduct = 0) { if (is_array($array)) { return (0 == count($array) ? $emptyProduct : array_reduce($array, '_array_product', 1)); } else { trigger_error('Param #1 must be an array', E_USER_ERROR); return false; } } function _array_product($v,$w) { return $v * $w; } } ?>

« previous php.notes (#198973) next »