The PHP array

PHP Fundamentals

Published: Jul 3, 2021 by C.S. Rhymes

PHP Fundamentals

This post is part of a series of posts about the fundamentals of PHP.

Arrays are a useful toolt to store multiple values. You can access a specific value in an array using a key. By default, if you don’t specify keys, they will be numeric and start at zero (not one);

$myArray = ['Red', 'Blue', 'Green'];

echo $myArray[0]; // Red
echo $myArray[1]; // Blue
echo $myArray[2]; // Green

If you try and use an array key that doesn’t exist then you will get a PHP notice.

echo $myArray[3]; // Notice: Undefined offset: 3

Alternative syntax

The short way of defining an array is using square brackets, but it’s useful to know the other syntax too, with the word array and brackets.

$myArray = array('Red', 'Blue', 'Green');

Add a new value to an array

You can add a new value to an array using empty square brackets, which will add the new value to the end of the array.

$myArray = ['Red', 'Blue', 'Green'];

$myArray[] = 'Yellow';

var_dump($myArray);
// array(4) { [0]=> string(3) "Red" [1]=> string(4) "Blue" [2]=> string(5) "Green" [3]=> string(6) "Yellow" }

Removing a value from an array

You can remove an item from an array using unset().

$myArray = ['Red', 'Blue', 'Green', 'Yellow'];

unset($myArray[3]);

var_dump($myArray); 
// array(3) { [0]=> string(3) "Red" [1]=> string(4) "Blue" [2]=> string(5) "Green" }

You can also use array_splice() to remove an item from an array. This takes the array as the first argument, the offset or where to start, and the length or how many items you want to remove.

$myArray = ['Red', 'Blue', 'Green', 'Yellow'];

array_splice($myArray, 3, 1);

var_dump($myArray); 
// array(3) { [0]=> string(3) "Red" [1]=> string(4) "Blue" [2]=> string(5) "Green" }

You can also specify a forth argument with array_splice() to add a replacement value to the array at the same time as removing values.

$myArray = ['Red', 'Blue', 'Green', 'Yellow'];

array_splice($myArray, 3, 1, 'Purple');

var_dump($myArray); 
// array(4) { [0]=> string(3) "Red" [1]=> string(4) "Blue" [2]=> string(5) "Green" [3]=> string(6) "Purple" }

Specifying keys

You can specify the keys if you want to, using the format ['key' => 'value'], defining this key has this value.

$myArray = [
    'red' => 'Red', 
    'blue' => 'Blue', 
    'green' => 'Green',
];

echo $myArray['red']; // Red

Multi-dimensional array

You can also have a multi-dimensional array, an array that contains an array. You can access values using the keys, one after the other, such as $myArray['top-level-key']['next-level-key'].

$myArray = [
    'red' => [
        'label' => 'Red',
        'hexcode' => '#FF0000',
    ],
    'blue' => [
        'label' => 'Blue',
        'hexcode' => '#0000FF',
    ],
    'green' => [
        'label' => 'Green',
        'hexcode' => '#00FF00',
    ]
];

echo $myArray['red']['label']; // Red
echo $myArray['red']['hexcode']; // #FF0000

Looping over an array

One thing that is very useful with an array is the ability to iterate or loop over it. If we use the same example as above we can use a foreach loop to do this.

foreach ($myArray as $colour) {
    echo "<p>{$colour['label']}: {$colour['hexcode']}</p>";
}

// Red: #FF0000
// Blue: #0000FF
// Green: #00FF00

Array functions

There are many built in functions for PHP arrays so if you are using an array in your code you can probably find a function that will do what you need, or even combine multiple functions to achieve what you need.

For more information on PHP functions, you can check out the array functions PHP documentation.

PHP Beginner Code

Share

Latest Posts

Using prettier to format your Jekyll theme
Using prettier to format your Jekyll theme

I have been using prettier for a few years to automatically format code, especially JavaScript and TypeScript projects, as it helps standardise the output on a shared code project. I have maintained a few different Jekyll themes over the years and wanted to use the power of prettier to automatically format code consistently.

First impressions of React Native as a Web Developer
First impressions of React Native as a Web Developer

I decided to try out React Native to build a native app and see how it works. I’m an experienced web developer but have limited knowledge of apps. I helped build an app using Apache Cordova and PhoneGap many (many) years ago and also worked on another app using the ionic framework a few years later. If I’m honest I can’t really remember how they worked as it was so long ago, so it was kind of like starting fresh for me.

Querying Laravel Eloquent's Many to Many relationships
Querying Laravel Eloquent's Many to Many relationships

A while ago I wrote about how to use a many to many relationship in Laravel, (all the way back in April 2019). Now I want to build on this example and show how you can query many to many relationships, but also how you can add additional constraints to the query to further filter your results.

How NOT to make a website

How NOT to make a Website

By C.S. Rhymes

From £2.49

Nigel's Intranet Adventure

Nigel's Intranet Adventure

By C.S. Rhymes

From £0.99