<?php $testArray = array(3, 2, 4, 7); echo "product is " . array_product($testArray); // product is 168 ?>
Tag Archives: String functions
Function to replace array elements – PHP
<?php $testArray = array("orange", "banana", "apple", "raspberry"); $replaceArr1 = array(0 => "pineapple", 3 => "cherry"); $replaceArr2 = array(0 => "grape"); $basket = array_replace($testArray, $replaceArr1, $replaceArr2); print_r($basket); //Array ( [0] => grape [1] => banana [2] => apple [3] => cherry ) ?>
Find out the difference of arrays – PHP
<?php $testArray1 = array("key1" => "val1", "key2" => "val2", "val4"); $testArray2 = array("key4" => "val3", "val2", "val7"); $testArray3 = array_diff($testArray1, $testArray2); print_r($testArray3);// Array ( [key1] => val1 [0] => val4 ) ?>
Function to fill an array with specified values – PHP
<?php $testArray = array_fill(5, 3, 'Value1'); print_r($testArray); //Array ( [5] => Value1 [6] => Value1 [7] => Value1 ) ?>
Pad values to an array with specified length – PHP
<?php $testArray = array(12, 10, 9); print_r( array_pad($testArray, 6, 0)); // Array ( [0] => 12 [1] => 10 [2] => 9 [3] => 0 [4] => 0 [5] => 0 ) ?>
Fetch previous element of an array – PHP
<?php $testArray = array('foot', 'bike', 'car', 'plane'); next($testArray); next($testArray); echo prev($testArray); // bike ?>
Fetch current element of an array – PHP
<?php $testArray = array('foot', 'bike', 'car', 'plane'); echo current($testArray); // foot ?>
Find out the next element of an array – PHP
<?php $testArray = array('foot', 'bike', 'car', 'plane'); secho next($testArray); // bike ?>
Function to exchanges keys with their associated values in an array – PHP
<?php $testArray = array("key1" => "Value1","key2" => "Value2","key3" => "Value3"); $testArray = array_flip($testArray); print_r($testArray); //Array ( [Value1] => key1 [Value2] => key2 [Value3] => key3 ) ?>
Sum of elements of an array – PHP
<?php $testArray = array(5, 3, 8, 10); echo "Sum is : ".array_sum($testArray) ; //Sum is : 26 ?>