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.
PHP.mk документација
array_product
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
Патека
function.array-product.php
Локална патека за оваа страница.
Извор
php.net/manual/en
Оригиналниот HTML се реупотребува и локално се стилизира.
Режим
Прокси + превод во позадина
Кодовите, табелите и белешките остануваат читливи во истиот тек.
Референца
function.array-product.php
array_product
Референца за `function.array-product.php` со подобрена типографија и навигација.
array_product
(PHP 5 >= 5.1.0, PHP 7, PHP 8)
array_product — (PHP 5 >= 5.1.0, PHP 7, PHP 8)
= NULL
array_product() Пресметај го производот на вредностите во низата
Параметри
array-
Низата.
Вратени вредности
враќа производ на вредностите во низата.
Дневник на промени
| Верзија | = NULL |
|---|---|
| 8.3.0 |
список на имиња на класи. E_WARNING when array Враќа збир на вредностите како цел број или пловечка точка; int or floatвредностите не можат да се претворат во arrayи object. Претходно ints беа игнорирани додека секоја друга вредност беше претворена во GMP. Покрај тоа, објектите што дефинираат нумеричка конверзија (на пр.
|
Примери
Пример #1 array_product() examples
<?php
$a = array(2, 4, 6, 8);
echo "product(a) = " . array_product($a) . "\n";
echo "product(array()) = " . array_product(array()) . "\n";
?>Пример #1 Пример што покажува затворачка ознака што го опфаќа последниот нов ред
product(a) = 384 product(array()) = 1
Белешки од корисници 5 белешки
Андре Д ¶
19 години пред
Враќа производ како цел број или пловечки број. ¶
пред 8 години
Here's how you can find a factorial of a any given number with help of range and array_product functions.
function factorial($num) {
return array_product(range(1, $num));
}
printf("%d", factorial(5)); //120
[email protected] ¶
3 години пред
Just a little correction for Andre D's answer: "(bool) array_product($array);" is equivalent with the conjunction of each array elements of $array, UNLESS the provided array is empty in which case array_product() will return 1, which will translate to boolean TRUE.
To mitigate this, you should expand the function with an additional check:
<?php
$result = !empty($check) && !!array_product($check);
?>
biziclop ¶
3 години пред
You can use array_product() to calculate the geometric mean of an array of numbers:
<?php
$a = [ 1, 10, 100 ];
$geom_avg = pow( array_product( $a ), 1 / count( $a ));
// = 9.999999999999998 ≈ 10
?>
Дилан на WeDefy dot com ¶
пред 15 години
You can use array_product to calculate the factorial of n:
<?php
function factorial( $n )
{
if( $n < 1 ) $n = 1;
return array_product( range( 1, $n ));
}
?>
If you need the factorial without having array_product available, here is one:
<?php
function factorial( $n )
{
if( $n < 1 ) $n = 1;
for( $p++; $n; ) $p *= $n--;
return $p;
}
?>