array_product


php128 apg

CALCULATE the PRODUCT of values in an ARRAY.

This function is compatible with LOCALE in PHP version 7.4.XX, however in PHP version 8.0.XX it is no longer supported.





This function returns the PRODUCT of values as INTEGER or FLOAT.

This funtion returns 1 if the $array is EMPTY.



<?php

nbr array_product 
arr $array )
 

where,

$array The input ARRAY

?> 

$array


The input ARRAY.



  1 EXERCISE   

<?php

$arr01 
= [ 2.8768.018.9132.12 ];

echo 
'The given ARRAY:<br>';

print_r($arr01);

echo 
'<br><br>The PRODUCT: ';

setlocale(LC_ALL'pt''pt-br''pt_BR');

$sarr01 array_product($arr01);

echo 
$sarr01 '<br>';

?> 

 RESULT   

PHP 7.4.XX
The given ARRAY:
Array ( [0] => 2.876 [1] => 8 [2] => 18.91 [3] => 32.12 )

The PRODUCT: 13974,8107136

ACCORDING THE BRAZILIAN LOCALE


PHP 8.0.XX
The given ARRAY:
Array ( [0] => 2.876 [1] => 8 [2] => 18.91 [3] => 32.12 )

The PRODUCT: 13974.8107136

NOT COMPATIBLE WITH LOCALE


  2 EXERCISE   

<?php

$arr02 
= [ 'two point six hundred and forty-two'
              
'eight point three hundred and thirty-three'33.47 ];

echo 
'The given ARRAY:<br>'

print_r($arr02);

echo 
'<br><br>The PRODUCT: '

$sarr02 array_product($arr02);

echo 
$sarr02 '<br>';

?> 

 RESULT   

The given ARRAY:
Array ( [0] => two point six hundred and forty-two [1] => eight point three hundred and thirty-three [2] => 33.47 )

The PRODUCT: 0


  3 EXERCISE   

<?php

$arr03 
= [ TRUE
                
2
               
'3.44'
               
7.98034
               [
2,4,6] ];

echo 
'The given ARRAY:<br>';  

print_r($arr03);

echo 
'<br><br>The PRODUCT: ';  

$sarr03 array_product($arr03);

echo 
$sarr03 '<br>';

?> 

 RESULT   

The given ARRAY:
Array
(
[0] => 1
[1] => 2
[2] => 3.44
[3] => 7.98034
[4] => Array
(
[0] => 2
[1] => 4
[2] => 6
)

)


The PRODUCT: 54.9047392


  4 EXERCISE   

<?php

$arr04 
= [ TRUE
              
false,
               
NULL,
                
2
               
'3.44'
               
7.98044
               
"SUM",
               [
2,4,6] ];

echo 
'The given ARRAY:<br>';  

print_r($arr04);

echo 
'<br><br>The PRODUCT: ';  

$sarr04 array_product($arr04);

echo 
$sarr04 '<br>';

?> 

 RESULT   

The given ARRAY:
Array
(
[0] => 1
[1] =>
[2] =>
[3] => 2
[4] => 3.44
[5] => 7.98034
[6] => SUM
[7] => Array
(
[0] => 2
[1] => 4
[2] => 6
)

)


The PRODUCT: 0


  5 EXERCISE   

<?php

$arr05 
= [ "continents" => ["South America""Europe"],
                  
"countries" => ["Brasil""Portugal"]]; 

echo 
'The given ARRAY:<br>';

print_r($arr05);

echo 
'<br><br>The PRODUCT: '

$sarr05 array_product($arr05);

echo 
$sarr05 '<br>';

?> 

 RESULT   

The given ARRAY:
Array
(
[continents] => Array
(
[0] => South America
[1] => Europe
)

[countries] => Array
(
[0] => Brasil
[1] => Portugal
)

)


The PRODUCT: 1


  6 EXERCISE   

<?php

$arr06 
= []; 

echo 
'The given ARRAY:<br>'

print_r($arr06);

echo 
'<br><br>The PRODUCT: ';

$sarr06 array_product($arr06);

echo 
$sarr06 '<br>';

?> 

 RESULT   

The given ARRAY:
Array ( )

The PRODUCT: 1


  7 EXERCISE   

<?php

echo "Funtion with a keyed array array:<br><br>";
               
$arr07 = ["bob" => 2"janet" => ];

echo 
'The given ARRAY:<br>'

print_r($arr07);

echo 
'<br><br>The PRODUCT: ';

$sarr07 array_product($arr07);

echo 
$sarr07 ' ---> '

               
var_dump($sarr07) . '<br>';

?>

  8 EXERCISE   

<?php

echo "Negative numbers:<br><br>";

$arr08a = array(-2);

$arr08b = array(-2, -3);

$arr08c = array(-2, -3, -4);

$arr08d = array(-1.5);

$arr08e = array(-99999999.999999999.1);


function 
arrproduct ($var08)
{
    echo 
'- - - - - - - - - - - - - - - - - - - - - - - - - - -
    <br>The given ARRAY:<br>'
;
    
print_r($var08);
    echo 
'<br><br>The PRODUCT of therms:<br>';
    
$prod08 array_product($var08);
    echo 
$prod08 ' ---> ';
    
var_dump($prod08); 
    echo 
'<br>- - - - - - - - - - - - - - - - - - - - - - - - - - -<br>'
}    

arrproduct($arr08a);

arrproduct($arr08b);

arrproduct($arr08c);

arrproduct($arr08d);

arrproduct($arr08e);

?>

  9 EXERCISE   

<?php

echo "Function with a very large array:<br><br>";

$array = array();

for (
$i 0$i 99$i++) {
  
$array[] = 11990;
}

echo 
'The given ARRAY:<br><pre>'
print_r($array);

echo 
'</pre><br><br>The PRODUCT of therms:<br>'
var_dumparray_product($array) );

?>