|
array_product
Calculate the product of values in an array
(PHP 5 >= 5.1.0)
Example 270. array_product() examples<?php The above example will output: product(a) = 384 Code Examples / Notes » array_productbishop
Yet another implementation of array_product() using PHP's native array_reduce(): if (! function_exists('array_product')) { function array_product($array) { if (is_array($array)) { return (0 == count($array) ? 0 : 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; } } andre d
This function can be used to test if all values in an array of booleans are TRUE. Consider: <?php function outbool($test) { return (bool) $test; } $check[] = outbool(TRUE); $check[] = outbool(1); $check[] = outbool(FALSE); $check[] = outbool(0); $result = (bool) array_product($check); // $result is set to FALSE because only two of the four values evaluated to TRUE ?> The above is equivalent to: <?php $check1 = outbool(TRUE); $check2 = outbool(1); $check3 = outbool(FALSE); $check4 = outbool(0); $result = ($check1 && $check2 && $check3 && $check4); ?> This use of array_product is especially useful when testing an indefinite number of booleans and is easy to construct in a loop. bishop
Regarding Andre D function to test if all values in an array of booleans are true, you can also use: <?php $allTrue = (! in_array(false, $arrayToCheck)); ?> Both this method and Andre D's are O(n), but this method has a lower k in the average case: in_array() stops once it finds the first false, while array_product must always traverse the entire array. gmail
Just in relation to "bishop" and the overall behaviour of array_product... The "empty product" (i.e. product of no values) is supposed to be defined as "1": http://en.wikipedia.org/wiki/Empty_product ...however PHP's array_product() returns int(0) if it is given an empty array. bishop's code does this, too (so it IS a compatible replacement). Ideally, array_product() should probably return int(1). I guess it depends on your specific context or rationale. You might normally presume int(0) to be a suitable return value if there are no inputs, but let's say that you're calculating a price based on "percentage" offsets: $price = 10.0; $discounts = get_array_of_customer_discounts(); $price = $price * array_product($discounts); ...if there are NO "discounts", the price will come out as 0, instead of 10.0 mattyfroese
If you don't have PHP 5 $ar = array(1,2,3,4); $t = 1; foreach($ar as $n){ $t *= $n; } echo $t; //output: 24 marcel
if you don't have PHP 5.xx . you can use this function. It does not make sure that the variables are numeric. function calculate_array_product($array="") { if(is_array($array)) { foreach($array as $key => $value) { $productkey = $productkey + $key; } return $productkey; } return NULL; } pqpqpq
An observation about the _use_ of array_product with primes: $a=$arrayOfSomePrimes=(2,3,11); // 2 being the first prime (these days) $codeNum=array_product($a); // gives 66 (== 2*3*11) echo "unique product(\$a) = " . array_product($a) . "\n"; The 66 can (only) be split into its original primes, which can be transformed into their place in the row of primes (2,3,5,7,11,13,17,19...) giving (1,2,3,4,5,6,7,8...) The 66 gives the places {1,2,5} in the row of primes. The number "66" is unique as a code for {1,2,5} So you can define the combination of table-columns {1,2,5} in "66". The bigger the combination, the more efficient in memory/transmission, the less in calculation. |
Change Languagearray_change_key_case array_chunk array_combine array_count_values array_diff_assoc array_diff_key array_diff_uassoc array_diff_ukey array_diff array_fill_keys array_fill array_filter array_flip array_intersect_assoc array_intersect_key array_intersect_uassoc array_intersect_ukey array_intersect array_key_exists array_keys array_map array_merge_recursive array_merge array_multisort array_pad array_pop array_product array_push array_rand array_reduce array_reverse array_search array_shift array_slice array_splice array_sum array_udiff_assoc array_udiff_uassoc array_udiff array_uintersect_assoc array_uintersect_uassoc array_uintersect array_unique array_unshift array_values array_walk_recursive array_walk array arsort asort compact count current each end extract in_array key krsort ksort list natcasesort natsort next pos prev range reset rsort shuffle sizeof sort uasort uksort usort |