<?php $testArray = array_fill(5, 3, 'Value1'); print_r($testArray); //Array ( [5] => Value1 [6] => Value1 [7] => Value1 ) ?>
Tag Archives: Array
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 ?>
function to checks whether given key or index exists in the array – PHP
<?php
$search_array = array('key1' => 17, 'key2' => 74);
if (array_key_exists('key1', $search_array)) {
echo "The key 'key1' exists ";
}
//The key 'key1' exists
?>
Function to find out the number of occurrence elements in an array -PHP
<?php
$testArray=array("Value1","Value1","Value1","Value2");
print_r(array_count_values($testArray));
//Array ( [Value1] => 3 [Value2] => 1 )
?>
Creates an array by using one array for keys and another for its values – PHP
<?php
$testArray1 = array('key1', 'key2', 'key3');
$testArray2 = array('value1', 'value2', 'value3');
$testArray3 = array_combine($testArray1, $testArray2);
print_r($testArray3);
//Array ( [key1] => value1 [key2] => value2 [key3] => value3 )
// NOTE : Make sure that the 2 arrays have equual
// number of elements
?>