Zend PHP 7 Certification – Arrays – Associative Arrays

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

Associative arrays are arrays that have named keys. So rather than having numeric keys, we can have keys that are composed as strings.

$array = array(0 => 'Jaguar', 1 => '5.4'); // Numeric
$array2 = array('make' => 'Jaguar', 'engine' => '5.4');

As of PHP 5.4 similar to numeric arrays, it is possible to use a short array syntax, which replaces array() with [].

$cars = array("Volvo","BMW");
$cars = ["Volvo","BMW"];

Adding and removing elements to and from arrays in PHP is easy, and a few in-built methods, array_push(), array_unshift(), array_pop() and array_shift() help achieve this.

The array_push() function will push one of more elements onto the end of an array. The first parameter of array_push() is the array itself, followed by values to be added to the array.

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

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

Instead of using array_push(), you can also add an element by using the syntax below.

$stack = array("orange", "banana");
$stack[] = 'raspberry';
print_r($stack);

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

Note that this method is used for enumerated arrays. Since there is no array_push() equivalent for associative arrays, there is no way determine the next key.

$stack = array("orange", "banana");
$stack['test'] = 'raspberry';
print_r($stack);

// Outputs:
Array ( [0] => orange [1] => banana [test] => raspberry )

As it turns out, using $stack[$key] = $value is faster than array_push(), so unless you’re using a return value of array_push(), it is advised to use the $stack[$key] = $value.

The array_unshift() 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);

For associative arrays, use the union operator to prepend a key/value pair to the array.

<?php
$original_array = array('word1' => 'something', 'word2' => 'something else');
$new_array = array('word0' => 'before something') + $original_array;
print_r($new_array);

// Outputs:
Array ( [word0] => before something [word1] => something [word2] => something else )

array_pop() ‘pops’ and returns the last value of the array, shortening the array by one element.

The array_pop() function takes one parameter which is the array itself.

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

Note that in the example raspberry gets assigned to $fruit.

print_r($fruit); // Outputs: raspberry

The array_shift() function shifts the first value of the array off and returns it, shortening the array by one element and moving everything down.

array_shift() takes one parameter which is the array itself.

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

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

Using the above methods, you can perform simple array manipulation to return data to suit your needs.

View the other sections:

Note: This article is based on PHP version 7.0.