Link Search Menu Expand Document

Arrays and Hashes

PHP programmers often reach for two main composite data structures for passing data around, Arrays and Hashes.

Arrays in PHP are similar to the arrays you’ve seen in other languages in that they allow you to store data in zero-based indexed collections.

Hashes (sometimes called associative arrays) also collections, but here the programmer defines the indices to use. Hashes are said to be collections of key and value pairs.

Table of Contents

  1. Arrays
  2. Count The Length
  3. Implode and Explode
  4. Inspecting Arrays with Print_r
  5. Hashes
  6. Isset() For Hashes
  7. Nesting - Array of Hashes
  8. Nesting - Hash of Arrays
  9. Helpful Array Functions

Arrays

Arrays are created using square braces. The data stored in an array can be retrieved using a zero-based index.

<?php

$numbers      = [1,2,3];
$to_do_list   = ['finish your homework','bathe','cook dinner'];
$to_do_list[] = 'practice taxidermy'; // Appended the array.
echo "You should {$to_do_list[0]} now!";

?>

Output:

You should finish your homework now!

The length of an Array need never be specified explicitly.

Resources

Count The Length

The length of an array can be determined using the count() function.

<?php
    $bookshelf = ['Catcher in the Rye',
                  'Anathem',
                  'The Glassbead Game',
                  'The Manticore'];
    echo "I own " . count($bookshelf) ." books.";
?>

Output:

 I own 4 books.

Resources

Implode and Explode

The implode() function joins array elements into a string, while the explode() function splits a string into an array.

Both functions need to be supplied with a delimiter.

<?php
    $poem = ['Mares eat oats','goat eat oats','little lambs eat ivy.'];
    echo implode(' and ', $poem) . "\n";

    $the_numbers  = '4,8,15,16,23,42';
    $dharma_hatch = explode(',',$the_numbers);
?>

Output:

Mares eat oats and goat eat oats and little lambs eat ivy.

Resources

Inspecting Arrays with Print_r

The print_r() function prints human-readable information about an array.

<?php
    $the_numbers  = '4,8,15,16,23,42';
    $dharma_hatch = explode(',',$the_numbers);
    print_r($dharma_hatch);
?>

Output:

Array
(
    [0] => 4
    [1] => 8
    [2] => 15
    [3] => 16
    [4] => 23
    [5] => 42
)

For even more details use var_dump() instead of print_r.

Resources

Hashes

A hash (sometimes called a map or a dictionary) is similar to an array. However, instead of storing and retrieving data using an index, hashes use keys to retrieve values. Each key is unique, and corresponds to a single value within the array. We call these key-value pairs.

In PHP, arrays are actually just hashes where the keys are zero-based integers.

Hash keys can also be strings.

<?php
    $french_fruit = ['apple'      => 'une pomme',
                     'pineapple'  => 'un ananas',
                     'grapefruit' => 'un pamplemousse'];
    echo "Aujourd'hui, je vais manger {$french_fruit['grapefruit']}.";
?>

Output:

Aujourd'hui, je vais manger un pamplemousse.

Isset() For Hashes

We can also use isset() to test if particular hash keys have been assigned values.

<?php
    function toy_box_contains($toy, $toy_box) {
        if (isset($toy_box[$toy])) {
            echo "You have {$toy_box[$toy]} {$toy}.\n";
        } else {
            echo "You have no $toy.";
        }
    }
    $toy_box = ['tin soldiers'   => 52,
                'pick-up sticks' => 35,
                'dice'           =>  6,
                'transformers'   =>  4];
    toy_box_contains('dice', $toy_box);
    toy_box_contains('robots', $toy_box);
?>

Output:

You have 6 dice.
You have no robots.

Nesting - Array of Hashes

Hashes can contain hashes. Arrays can contain arrays. Hashes can contain Arrays. Arrays can contain Hashes.

Here’s an array where the elements are hashes:

<?php
    // An Array of Hashes
    $employees = [
                     ['name'     => 'Wally Glutton',
                      'position' => 'Ninja'],
                     ['name'     => 'Jane McGonigal',
                      'position' => 'Instructor']
                 ];
    echo "{$employees[0]['name']} is a {$employees[0]['position']}.";
?>

Output:

Wally Glutton is a Ninja.

Nesting - Hash of Arrays

Here’s a hash where the values are arrays:

<?php
    // A Hash of Arrays
    $favourite_foods = [ 'jane' => ['cabbage','pizza'],
                         'john' => ['rainbow trout','chalk']];

    $john_eats = $favourite_foods['john'];
    echo "John eats {$john_eats[0]} and {$john_eats[1]}.";
?>

Output:

John eats rainbow trout and chalk.

Two things that should also be noted:

  • The contents of an array or hash can be of non-uniform type.
  • The print_r() function also works for hashes.

For example:

<?php
    $mixed_bag = ['one', 2, 'three', 4, [5 => 'five', 'six' => 6]];
    print_r($mixed_bag);
?>

Output:

Array
(
    [0] => one
    [1] => 2
    [2] => three
    [3] => 4
    [4] => Array
        (
            [5] => five
            [six] => 6
        )
)

Helpful Array Functions

  • in_array - Checks if a value exists in an array.
  • array_key_exists - Checks if the given key or index exists in the array.
  • shuffle - Shuffles an array.
  • sort - Sort an array.
  • asort - Sort an hash and maintain index association.
  • list - Assign variables as if they were an array.