Arrays in PHP
Let's figure out what an array in PHP is. An array is simply a group of values presented as a whole. Imagine a basket of fruits. It contains a banana, an orange, an apple, and grapes. In PHP, you can represent such a structure as an array. The basket is the array itself, and the specific fruits are its elements.
Now let's create a file in our project folder for our experiments with arrays. We'll name it arrays.php.
An array in PHP is declared using square brackets, like this:
$fruits = [];
This is how we've created an empty array.
In older code, you might come across this way of defining an array:
$fruits = array();
This notation is now outdated and should not be used!
You can also create an array that already contains some values. This is done as follows:
$fruits = ['apple', 'orange', 'grape'];
To display the resulting array, we can use a function we are already familiar with, var_dump:
<?php
$fruits = ['apple', 'orange', 'grape'];
var_dump($fruits);
And let's run this script by opening it in a browser: http://myproject.loc/arrays.php
We will see the following:
array(3) { [0]=> string(5) "apple" [1]=> string(6) "orange" [2]=> string(5) "grape" }
Numbered Arrays
Array is an object type, specifically an array. Size=3 indicates the size of the array (it contains 3 elements). 0, 1, 2 are the array keys, also known as indices. These keys store values, in our case, these values are strings. As you can see, the array keys start from zero and then simply increment by one.
To retrieve one of the array values, you can use these keys. For this, you specify the name of the array and then in square brackets, you provide the key for which you want the value.
For example, to get the zeroth element of the array (with key 0), you would do the following:
<?php
$fruits = ['apple', 'orange', 'grape'];
echo $fruits[0];
The result of this code:
apple
Let's get the element with index 2:
<?php
$fruits = ['apple', 'orange', 'grape'];
echo $fruits[2];
Result:
grape
If we try to retrieve an element with a non-existent index, for example – 3:
<?php
$fruits = ['apple', 'orange', 'grape'];
echo $fruits[3];
We will get a warning that an element with such a key is not found.
Adding and Removing Array Elements
Let's add another element to the array, using the following construction:
$fruits[] = 'mango';
Let's look at our array again using var_dump after this:
<?php
$fruits = ['apple', 'orange', 'grape'];
$fruits[] = 'mango';
var_dump($fruits);
The result – another element with index 3:
array(4) { [0]=> string(5) "apple" [1]=> string(6) "orange" [2]=> string(5) "grape" [3]=> string(5) "mango" }
To remove elements from an array, the unset construction is used. Let's remove the element with index 2:
<?php
$fruits = ['apple', 'orange', 'grape'];
$fruits[] = 'mango';
unset($fruits[2]);
var_dump($fruits);
The result of this code:
array(3) { [0]=> string(5) "apple" [1]=> string(6) "orange" [3]=> string(5) "mango" }
As we see, there is no longer an element with index 2, and a "gap" has formed in the sequence of key numbers. If a new element is added now, its index will be 4, but the gap will remain. This is how it works and it's important to keep in mind.
And Back to Keys
Actually, these keys can be set by yourself, even when creating the array. Like this:
<?php
$fruits = [5 => 'apple', 3 => 'orange', 9 => 'grape'];
var_dump($fruits);
Result:
array(3) { [5]=> string(5) "apple" [3]=> string(6) "orange" [9]=> string(5) "grape" }
As we can see, the keys are now 5, 3, and 9.
If we now add an element to the array, its index will be one more than the maximum numeric value of the key:
<?php
$fruits = [5 => 'apple', 3 => 'orange', 9 => 'grape'];
$fruits[] = 'mango';
var_dump($fruits);
Result:
array(4) { [5]=> string(5) "apple" [3]=> string(6) "orange" [9]=> string(5) "grape" [10]=> string(5) "mango" }
You can also add values to an array using a specific key, for example, we want to add mango to the array and make it stored under index 20. It's as simple as this:
<?php
$fruits = [5 => 'apple', 3 => 'orange', 9 => 'grape'];
$fruits[20] = 'mango';
var_dump($fruits);
Result:
array(4) { [5]=> string(5) "apple" [3]=> string(6) "orange" [9]=> string(5) "grape" [20]=> string(5) "mango" }
If a value with such a key already exists in the array, it will simply be replaced with a new one. For example, let's replace the apple with mango:
<?php
$fruits = [5 => 'apple', 3 => 'orange', 9 => 'grape'];
var_dump($fruits);
$fruits[5] = 'mango';
var_dump($fruits);
Result:
array(3) { [5]=> string(5) "apple" [3]=> string(6) "orange" [9]=> string(5) "grape" }
array(3) { [5]=> string(5) "mango" [3]=> string(6) "orange" [9]=> string(5) "grape" }
Associative Arrays
In addition to numerical keys, you can use ordinary strings. Such arrays are called associative arrays. Let's imagine a situation: there is an article with a title, text, and an author. It can easily be represented as an array. Let's do it:
<?php
$article = ['title' => 'The Title of the Article', 'text' => 'The text of the article'];
$article['author'] = 'The Author\'s Name';
var_dump($article);
The result of this code:
array(3) { ["title"]=> string(24) "The Title of the Article" ["text"]=> string(23) "The text of the article" ["author"]=> string(17) "The Author's Name" }
Great, and now we can use this inside HTML markup:
<?php
$article = [
'title' => 'The Title of the Article',
'text' => 'The text of the article',
'author' => 'The Author\'s Name'
];
?>
<html>
<head>
<title><?= $article['title'] ?></title>
</head>
<body>
<h1><?= $article['title'] ?></h1>
<p><?= $article['text'] ?></p>
<p><?= $article['author'] ?></p>
</body>
</html>
Multidimensional Arrays
Remember, at the beginning of the lesson, I said that the keys of an array store some values, and in our case, these values are strings. Well, an array element can be anything, even another array. =)
Let's use our article as an example – the author might have a first name and a last name. And we want to store them separately from each other. Then, the author is an array with two keys – first_name and last_name.
Let's do it:
<?php
$article = [
'title' => 'The Title of the Article',
'text' => 'The text of the article',
'author' => [
'first_name' => 'John',
'last_name' => 'Doe'
]
];
var_dump($article);
Result:
array(3) { ["title"]=> string(24) "The Title of the Article" ["text"]=> string(23) "The text of the article" ["author"]=> array(2) { ["first_name"]=> string(4) "John" ["last_name"]=> string(3) "Doe" } }
Just like that, article is an array, which has another array at the key author.
To get the author's first name, you use the following code:
<?php
$article = [
'title' => 'The Title of the Article',
'text' => 'The text of the article',
'author' => [
'first_name' => 'John',
'last_name' => 'Doe'
]
];
echo $article['author']['first_name'];
First, we got the value by the key author in the array $article, and that value turned out to be another array. Then from that array, we got the value by the key first_name. And the result of this code, of course:
John
Let's now use these values in the template we have already used:
<?php
$article = [
'title' => 'The Title of the Article',
'text' => 'The text of the article',
'author' => [
'first_name' => 'John',
'last_name' => 'Doe'
]
];
?>
<html>
<head>
<title><?= $article['title'] ?></title>
</head>
<body>
<h1><?= $article['title'] ?></h1>
<p><?= $article['text'] ?></p>
<p><?= $article['author']['first_name'] . ' ' . $article['author']['last_name'] ?></p>
</body>
</html>
Of course, you can create an array within this value, and within that one – another one, until you get tired.
In the next lesson, we will look at more complex and interesting examples of working with arrays.
Comments