Zend PHP 7 Certification – Arrays – Array Functions

This post covers the Array Functions section of the Arrays chapter when studying for the Zend PHP 7 Certification.

A list of some of the common functions used when dealing with arrays can be seen below.

array_push()

This function pushes one or more elements onto the end of array.

<?php
$stack = array("orange", "banana");
array_push($stack, "apple", "raspberry");
print_r($stack);

// Outputs:
Array
(
    [0] => orange
    [1] => banana
    [2] => apple
    [3] => raspberry
)

array_unshift()

This function prepends one or more elements to the beginning of an array.

<?php
$queue = array("orange", "banana");
array_unshift($queue, "apple", "raspberry");
print_r($queue);

// Outputs:
Array
(
    [0] => apple
    [1] => raspberry
    [2] => orange
    [3] => banana
)

array_pop()

This function pops an element off the end of the array.

<?php
$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_pop($stack);
print_r($stack);

// Outputs:
Array
(
    [0] => orange
    [1] => banana
    [2] => apple
)

array_shift()

This function shifts an element off the beginning of array.

<?php
$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_shift($stack);
print_r($stack);

// Outputs:
Array
(
    [0] => banana
    [1] => apple
    [2] => raspberry
)

array_reverse()

This function returns an array with elements in reverse order. The second parameter passed in is a boolean that if true, preserves the array keys.

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

print_r($input);
print_r($reversed);
print_r($preserved);

// Outputs:
Array
(
    [0] => php
    [1] => 4
    [2] => Array
        (
            [0] => green
            [1] => red
        )

)
Array
(
    [0] => Array
        (
            [0] => green
            [1] => red
        )

    [1] => 4
    [2] => php
)
Array
(
    [2] => Array
        (
            [0] => green
            [1] => red
        )

    [1] => 4
    [0] => php
)

array_merge()

This function merges one or more arrays. If the input arrays have the same string keys, then the later value for that key will overwrite the previous one.

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

// Outputs:
Array
(
    [color] => green
    [0] => 2
    [1] => 4
    [2] => a
    [3] => b
    [shape] => trapezoid
    [4] => 4
)

array_diff()

This function compares array1 against one or more other arrays and returns the values in array1 that are not present in any of the other arrays.

<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);

print_r($result);

// Outputs:
Array
(
    [1] => blue
)

array_diff_assoc()

Compares array1 against array2 and returns the difference. Unlike array_diff() the array keys are also used in the comparison. Like array_diff(), it returns the values in array1 that are not present in any of the other arrays.

<?php
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_diff_assoc($array1, $array2);
print_r($result);

// Outputs:
Array
(
    [b] => brown
     => blue
    [0] => red
)

array_diff_key()

Compares the keys from array1 against the keys from array2 and returns the difference. This function is like array_diff() except the comparison is done on the keys instead of the values.

<?php
$array1 = array('blue'  => 1, 'red'  => 2, 'green'  => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan'   => 8);

var_dump(array_diff_key($array1, $array2));

// Outputs:
array(2) {
  ["red"]=>
  int(2)
  ["purple"]=>
  int(4)
}

sort()

This function sorts elements within an array. The first parameter is the input array, and the second parameter is a sorting flag. Some of the flags that can be used are:

  • SORT_REGULAR – compare items normally (don’t change types)
  • SORT_NUMERIC – compare items numerically
  • SORT_STRING – compare items as strings
  • SORT_LOCALE_STRING – compare items as strings, based on the current locale. It uses the locale, which can be changed using setlocale()
  • SORT_NATURAL – compare items as strings using “natural ordering” like natsort()
<?php
$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
foreach ($fruits as $key => $val) {
    echo "fruits[" . $key . "] = " . $val . "\n";
}

// Outputs:
fruits[0] = apple
fruits[1] = banana
fruits[2] = lemon
fruits[3] = orange

rsort()

This function sorts the array in reverse order.

<?php
$fruits = array("lemon", "orange", "banana", "apple");
rsort($fruits);
foreach ($fruits as $key => $val) {
    echo "$key = $val\n";
}

// Outputs:
0 = orange
1 = lemon
2 = banana
3 = apple

asort()

This function sorts an array and maintains index association.

<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
asort($fruits);
foreach ($fruits as $key => $val) {
    echo "$key = $val\n";
}

// Outputs:
c = apple
b = banana
d = lemon
a = orange

The fruits have been sorted in alphabetical order, and the index associated with each element has been maintained.

ksort()

This function sorts an array by key.

<?php
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
ksort($fruits);
foreach ($fruits as $key => $val) {
    echo "$key = $val\n";
}

// Outputs:
a = orange
b = banana
c = apple
d = lemon

usort()

This function sorts an array using a user defined comparison function.

<?php
function cmp($a, $b)
{
    if ($a == $b) {
        return 0;
    }
    return ($a < $b) ? -1 : 1;
}

$a = array(3, 2, 5, 6, 1);

usort($a, "cmp");

foreach ($a as $key => $value) {
    echo "$key: $value\n";
}

// Outputs:
0: 1
1: 2
2: 3
3: 5
4: 6

natsort()

This function sorts an array using a “natural order” algorithm.

<?php
$array1 = $array2 = array("img12.png", "img10.png", "img2.png", "img1.png");

asort($array1);
print_r($array1);

natsort($array2);
print_r($array2);

// Outputs:
Array
(
    [3] => img1.png
    [1] => img10.png
    [0] => img12.png
    [2] => img2.png
)

Array
(
    [3] => img1.png
    [2] => img2.png
    [1] => img10.png
    [0] => img12.png
)

View the other sections:

Note: This article is based on PHP version 7.0.