<?php
$testArray1 = array("place" => "kochi", 11, 12);
$testArray2 = array("p", "s", "place" => "Tvm", "Width" => "10", 84);
$testArray3 = array_merge($testArray1, $testArray2);
print_r($testArray3);
//Array ( [place] => Tvm [0] => 11 [1] => 12 [2] => p [3]
//=> s [Width] => 10 [4] => 84 )
?>
Tag Archives: Array
Convert the values of an array into uppercase – PHP
<?php
$testArray = array("Value1","Value2", "Value3");
print_r(array_map('strtoupper', $testArray));
// Array ( [0] => VALUE1 [1] => VALUE2 [2] => VALUE3 )
?>
Convert the keys of an array into uppercase – PHP
<?php
$testArray=array("key1"=>"Value1","key2"=>"Value2","key3"=>"Value3");
print_r(array_change_key_case($testArray,CASE_UPPER));
//Array ( [KEY1] => Value1 [KEY2] => Value2 [KEY3] => Value3 )
?>
Apply a function to all the element of an array – PHP
<?php
$testArray = array(2.4, 2.6, 3.5);
print_r(array_map('ceil', $testArray));
// Array ( [0] => 3 [1] => 3 [2] => 4 )
$testArray = array(2.4, 2.6, 3.5);
print_r(array_map('floor', $testArray));
// Array ( [0] => 2 [1] => 2 [2] => 3 )
?>
How to check whether a variable is an array – PHP
<?php
$array = array('this', 'is', 'an array');
echo is_array($array) ? 'True' : 'False'; // True
echo " ";
$var = 'this is a string';
echo is_array($no) ? 'True' : 'False'; // False
?>
Convert strings to an array – PHP
<?php
$testString = "FirstName,MiddleName,LastName";
$testArray = split(",",$testString);
print_r($testArray);
//Array ( [0] => FirstName [1] => MiddleName [2] => LastName )
?>
<?php
$testString = "FirstName MiddleName,LastName";
list($first, $middle, $last) = split('[ ,]', $testString);
echo "First Name: $first; MiddleName: $middle; LastName: $last";
?>
How to read all the keys or a subset of the keys of an array – PHP
<?php $testArray = array(1 => 5, "name" => "First Name"); print_r(array_keys($testArray)); //Array ( [0] => 1 [1] => name ) ?>
Randomizes the order of the elements in an array – PHP
$numbers = range(0, 9);
shuffle($numbers);
foreach ($numbers as $number) {
echo $number;
}
Create an array containing a range of elements – PHP
<?php $numbers = range(0, 9); foreach($numbers as $value) echo $value." "; //Will output 0 1 2 3 4 5 6 7 8 9 ?>
Reverse the order of elements in an array – PHP
<?php
$testData= array('Test1', 'Test2', 'Test3');
print_r(array_reverse($testData));
//Array ( [0] => Test3 [1] => Test2 [2] => Test1 )
?>