- PHP Array Product: A Comprehensive Guide
- Understanding the PHP Array Product Function
- How to Use the PHP Array Product Function
- Potential Pitfalls
- Wrapping Up
- array_product
- Список параметров
- Возвращаемые значения
- Примеры
- User Contributed Notes 7 notes
- PHP array_product() Function
- Syntax
- Parameter Values
- Technical Details
- More Examples
- Example
- COLOR PICKER
- Report Error
- Thank You For Helping Us!
- PHP array_product() Function
- Return value
- Example 1: How to Use PHP array_product function
- Example 2: Pass the integral and float values
- Example 3: Elements with 0 in array_product()
- Example 4: Array Elements Containing String
- Example 5: Checking if all the values are boolean true or not
- array_product() function in PHP
- Syntax
- Parameters
- Return
- Example
- Output
- Output
- Annual Membership
- Training for a Team
PHP Array Product: A Comprehensive Guide
The PHP array product function is an important tool for any PHP developer to have in their toolkit. It calculates the product of all elements in an array and returns the result. This can be incredibly useful in a variety of situations, such as when working with mathematical operations or when trying to determine the overall value of a set of items.
Understanding the PHP Array Product Function
The PHP array product function takes in an array as its input and calculates the product of all of the elements in that array. This can be incredibly useful in a variety of situations, such as when working with mathematical operations or when trying to determine the overall value of a set of items.
The function works by starting with an initial value of 1 and then multiplying each subsequent element in the array by that value. The end result is the product of all of the elements in the array.
int array_product ( array $array )
How to Use the PHP Array Product Function
Using the PHP array product function is simple. First, you’ll need to create an array that you’d like to calculate the product of. Then, simply pass that array as an argument to the array_product function. The result will be returned to you as an integer.
$numbers = array(1, 2, 3, 4, 5); $product = array_product($numbers); echo $product; ?>
Potential Pitfalls
It’s important to note that the PHP array product function only works with arrays that contain numeric values. If you try to pass an array that contains non-numeric values, you’ll receive an error. Additionally, if you pass an empty array to the array_product function, the result will be 0.
$numbers = array("1", "2", "3", "4", "Q"); $product = array_product($numbers); echo $product; ?>
Wrapping Up
The PHP array product function is a useful tool for any PHP developer to have in their toolkit. Whether you’re working with mathematical operations or trying to determine the overall value of a set of items, this function can help you get the job done quickly and efficiently. So, be sure to give it a try the next time you find yourself in need of a quick and accurate way to calculate the product of an array.
array_product
array_product() возвращает произведение значений массива.
Список параметров
Возвращаемые значения
Возвращает произведение как тип integer или float.
Примеры
Пример #1 Примеры использования array_product()
$a = array( 2 , 4 , 6 , 8 );
echo «product(a) color: #007700″>. array_product ( $a ) . «\n» ;
echo «product(array()) color: #007700″>. array_product (array()) . «\n» ;
Результат выполнения данного примера:
product(a) = 384 product(array()) = 1
User Contributed Notes 7 notes
This function can be used to test if all values in an array of booleans are TRUE.
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:
$check1 = outbool ( TRUE );
$check2 = outbool ( 1 );
$check3 = outbool ( FALSE );
$check4 = outbool ( 0 );
?>
This use of array_product is especially useful when testing an indefinite number of booleans and is easy to construct in a loop.
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));
>
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:
You can use array_product() to calculate the geometric mean of an array of numbers:
$a = [ 1 , 10 , 100 ];
$geom_avg = pow ( array_product ( $a ), 1 / count ( $a ));
// = 9.999999999999998 ≈ 10
?>
You can use array_product to calculate the factorial of n:
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:
function factorial ( $n )
<
if( $n < 1 ) $n = 1 ;
for( $p ++; $n ; ) $p *= $n —;
return $p ;
>
?>
array_product() can be used to implement a simple boolean AND search
$args = array( ‘first_name’ => ‘Bill’ , ‘last_name’ => ‘Buzzard’ );
$values [] = array( ‘first_name’ => ‘Brenda’ , ‘last_name’ => ‘Buzzard’ );
$values [] = array( ‘first_name’ => ‘Victor’ , ‘last_name’ => ‘Vulture’ );
$values [] = array( ‘first_name’ => ‘Bill’ , ‘last_name’ => ‘Blue Jay’ );
$values [] = array( ‘first_name’ => ‘Bill’ , ‘last_name’ => ‘Buzzard’ );
$result = search_for ( $values , $args );
var_dump ( $result );exit;
function search_for ( $array , $args ) $results = array();
foreach ( $array as $row ) $found = false ;
$hits = array();
foreach ( $row as $k => $v ) if ( array_key_exists ( $k , $args )) $hits [ $k ] = ( $args [ $k ] == $v );
>
$found = array_product ( $hits );
if (! in_array ( $row , $results ) && true == $found ) $results [] = $row ;
>
array (size=1)
0 =>
array (size=2)
‘first_name’ => string ‘Bill’ (length=4)
‘last_name’ => string ‘Buzzard’ (length=7)
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 in the row of primes. The number «66» is unique as a code for
So you can define the combination of table-columns in «66». The bigger the combination, the more efficient in memory/transmission, the less in calculation.
- Функции для работы с массивами
- array_change_key_case
- array_chunk
- array_column
- 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_is_list
- array_key_exists
- array_key_first
- array_key_last
- array_keys
- array_map
- array_merge_recursive
- array_merge
- array_multisort
- array_pad
- array_pop
- array_product
- array_push
- array_rand
- array_reduce
- array_replace_recursive
- array_replace
- 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
- end
- extract
- in_array
- key_exists
- key
- krsort
- ksort
- list
- natcasesort
- natsort
- next
- pos
- prev
- range
- reset
- rsort
- shuffle
- sizeof
- sort
- uasort
- uksort
- usort
- each
PHP array_product() Function
The array_product() function calculates and returns the product of an array.
Syntax
Parameter Values
Technical Details
Return Value: Returns the product as an integer or float PHP Version: 5.1.0+ Changelog: As of PHP 5.3.6, the product of an empty array is 1. Before PHP 5.3.6, this function would return 0 for an empty array. More Examples
Example
Calculate and return the product of an array:
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.
PHP array_product() Function
$array: It refers to the input array whose products of elements we wish to get.
Return value
The array_product() function returns an integer or float value depending on the nature of the elements of the array.
Example 1: How to Use PHP array_product function
$arr = array(11, 18, 19, 21, 29); $product = array_product($arr); echo $product;
As of PHP 5.3.6, the product of an empty array is 1. Before PHP 5.3.6, this function would return 0 for an empty array.
Example 2: Pass the integral and float values
When an array passed to the array_product() function contains both integral and float values, then the array_product() function returns a floating-point value equal to the product of all the elements of the array passed to it.
$arr = array(1.1, 2.1); $product = array_product($arr); echo $product;
Example 3: Elements with 0 in array_product()
If any value in an array is 0, the function will always return 0.
$arr = array(11, 21, 19, 0, 46, 29); $product = array_product($arr); echo $product;
Example 4: Array Elements Containing String
If any element in the input array is the string, the function will type-case it to 0. That is why it will always return 0 in such a case.
$arr = array(11, 21, 19, 'Eleven', 46, 29); $product = array_product($arr); echo $product;
Example 5: Checking if all the values are boolean true or not
You can use a function to check whether all the values are true.
$arr = array(1, 11, 0, true); $product = array_product($arr); echo $product;
array_product() function in PHP
The array_product() function calculates the product of the values in an array. It returns the product of values.
Syntax
Parameters
Return
The array_product() function returns the product of values in the array. The value is returned as integer or float.
Example
The following is an example −
Output
The following is the output −
Let us see another example wherein we have a mix of integer and float elements in the array. The resultant product will be float −
Output
- Related Articles
- filter_has_var() function in PHP
- filter_id() function in PHP
- filter_input() function in PHP
- filter_input_array() function in PHP
- filter_list() function in PHP
- filter_var_array() function in PHP
- filter_var() function in PHP
- constant() function in PHP
- define() function in PHP
- defined() function in PHP
- die() function in PHP
- eval() function in PHP
- exit() function in PHP
- get_browser() function in PHP
- highlight_file() function in PHP
Annual Membership
Enjoy unlimited access on 5500+ Hand Picked Quality Video Courses
Training for a Team
Affordable solution to train a team and make them project ready.
- About us
- Refund Policy
- Terms of use
- Privacy Policy
- FAQ’s
- Contact
Copyright © Tutorials Point (India) Private Limited. All Rights Reserved.
We make use of First and third party cookies to improve our user experience. By using this website, you agree with our Cookies Policy. Agree Learn more