quick links

PHP Array and Function

An array is a special variable, which can hold more than one value at a time.

Array Syntax: $arrayName = array ("value1","value2","value3");

In PHP, there are three types of arrays:

  • Indexed arrays or Numeric arrays - Arrays with numeric index
  • Associative arrays - Arrays with named keys
  • Multidimensional arrays - Arrays containing one or more arrays

1) array_shift Function:

The array_shift function removes the first element of an array from the array and stores it in the receiving variable.

$myarray = array("Apples", "Oranges", "Pears");
$fruit = array_shift($myarray);  
echo $fruit; // Apples
print_r($myarray);  // Oranges and Pears will be displayed without Apples

2) array_unshift Function:

Prepend one or more elements to the beginning of an array.

$queue = array("orange", "banana");
array_unshift($queue, "apple", "raspberry");

3) array_push Function:

Push one or more elements onto the end of array.

$stack = array("orange", "banana");
array_push($stack, "apple", "raspberry");

4) array_pop Function:

Pop the element off the end of array.

$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_pop($stack);

5) array_combine Function:

Creates an array by using one array for keys and another for its values.

$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);

6) array_unique Function:

Removes duplicate values from an array.

$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);

7) array_merge Function:

Merge one or more arrays.

$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);

8) Arrays Sort Functions:

sort() - sort arrays in ascending order
rsort() - sort arrays in descending order
asort() - sort associative arrays in ascending order, according to the value
ksort() - sort associative arrays in ascending order, according to the key
arsort() - sort associative arrays in descending order, according to the value
krsort() - sort associative arrays in descending order, according to the key

9) array_replace Function:

Replaces elements from passed arrays into the first array

$base = array("orange", "banana", "apple", "raspberry");
$replacements = array(0 => "pineapple", 4 => "cherry");
$replacements2 = array(0 => "grape");
$basket = array_replace($base, $replacements, $replacements2);

10) array_reverse Function:

Return an array with elements in reverse order

$input  = array("php", 4.0, array("green", "red"));
$reversed = array_reverse($input);
$preserved = array_reverse($input, true);

11) array_search Function:

Searches the array for a given value and returns the first corresponding key if successful

$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array);   // $key = 1;

12) array key function:

array_keys — Return all the keys or a subset of the keys of an array
array_key_exists — Checks if the given key or index exists in the array

$search_array = array('first' => 1, 'second' => 4);
if (array_key_exists('first', $search_array)) {
echo "The 'first' element is in the array";

array_key_first — Gets the first key of an array
array_key_last — Gets the last key of an array

13) array_rand Function:

Pick one or more random keys out of an array

$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
$rand_keys = array_rand($input, 2);
echo $input[$rand_keys[0]] . "\n";
echo $input[$rand_keys[1]] . "\n";

14) array_sum Function:

Calculate the sum of values in an array
Returns the sum of values as an integer or float; 0 if the array is empty.

$a = array(2, 4, 6, 8);
echo "sum(a) = " . array_sum($a) . "\n";
$b = array("a" => 1.2, "b" => 2.3, "c" => 3.4);
echo "sum(b) = " . array_sum($b) . "\n";

About Us

Joomla Web Solution

We love working with PHP, HTML5, CSS3, jQuery and the most-loved content management framework in the world, Joomla!