With this lesson we are introducing a powerful PHP tool: the arrays! Welcome to Learning PHP – Lesson 4 – Learn PHP Arrays. In this lesson we will show how PHP arrays are declared, combined and merged. An array in PHP is an ordered map and is super flexible since can be used as list, dictionary, hash table, collection, stack, queue etc. If you are coming from python this is one of the things that probably you will get some issues with.
If you missed here is a list of other articles of the same series
Other articles of the same series
Topics we are discussing in this article
How to declare an array
In php you can declare an array in different ways
using the array() construct
<?php
$my_array = array ();
// OR
// key,value type
$my_array = array (
"article_type" => "blog post",
"title" => "Lesson 4 - Arrays",
"category" => "PHP"
);
// list type
$my_array = array (
2,3,4
);
// mixed type
$my_array = array (
2, 3, 4,
"article_type" => "blog post",
"title" => "Lesson 4 - Arrays",
"category" => "PHP"
);
?>
or using the short syntax
<?php
// empty array
$my_array = [];
// OR
// key,value type
$my_array = [
"article_type" => "blog post",
"title" => "Lesson 4 - Arrays",
"category" => "PHP"
];
// list type
$my_array = [
2,3,4
];
// mixed type
$my_array = [
2, 3, 4,
"article_type" => "blog post",
"title" => "Lesson 4 - Arrays",
"category" => "PHP"
];
Regardless of how the array are defined we can access by using the array index (that as any other language starts from 0. See below
<?php
$my_array = [
2, 3, 4,
"article_type" => "blog post",
"title" => "Lesson 4 - Arrays",
"category" => "PHP"
];
echo $my_array[0]; // will print 2
echo $my_array[2]; // will print 4
echo $my_array["category"]; // will print PHP
?>
In the same way you can define multidimensional arrays
<?php
$my_array = array(
"first" => array(1,2,3),
"second" => array(4,5,6),
"third" => array(7,8,9)
);
print_r($my_array);
// OR
$my_array = [
"first" => array(1,2,3),
"second" => array(4,5,6),
"third" => array(7,8,9)
];
print_r($my_array);
// OR
$my_array = [
"first" => [1,2,3],
"second" => [4,5,6],
"third" => [7,8,9]
];
print_r($my_array);
// OR
$first = [1,2,3];
$second = [4,5,6];
$third = [7,8,0];
$my_array["first"] = $first;
$my_array["second"] = $second;
$my_array["third"] = $third;
print_r($my_array);
?>
as you can see the result is always the same. It is important to note that in the last example, PHP is creating the array entry for “first”, “second” or “third” if it doesn’t exist yet.
Some useful functions
Combining arrays
For combining array we can use the array_combine() function. In some circumstances this function can be very handy. It combines the arrays sent as parameters in such a way that as result there will be an array which keys are the values of the first argument and as values, the values of the second array. See the example below:
<?php
//array combine example
$array_of_keys = ["first", "second", "third"];
$array_of_values = [1,2,3];
$my_array = array_combine($array_of_keys, $array_of_values);
print_r($my_array);
?>
Merging arrays
For merging one or more array we can use array_merge()
<?php
//array combine example
$array_1 = ["first", "second", "third"];
$array_2 = [1,2,3];
$array_3 = [];
$array_4 = [
"somekey" => "some value",
"some_other_key" => ["some", "other", "value"]
];
$my_array = array_merge($array_1, $array_2, $array_3, $array_4);
print_r($my_array);
?>
Count elements of an array
For counting elements we can use the count() function
<?php
echo count($my_array) . "\n";
?>
Pushing elements
For pushing one or more elements at the end of the array just use array_push()
<?php
//array combine example
$array_1 = ["first", "second", "third"];
$array_2 = [1,2,3];
$array_3 = [];
$array_4 = [
"somekey" => "some value",
"some_other_key" => ["some", "other", "value"]
];
$my_array = [];
array_push($my_array, $array_1);
array_push($my_array, $array_2);
array_push($my_array, $array_3);
array_push($my_array, $array_4);
print_r($my_array);
echo count($my_array) . "\n";
?>
You will soon notice that the result is not what expected. If you need only the values of an associative array you can leverage the array_values() function
<?php
//array combine example
$array_1 = ["first", "second", "third"];
$array_2 = [1,2,3];
$array_3 = [];
$array_4 = [
"somekey" => "some value",
"some_other_key" => ["some", "other", "value"]
];
$my_array = [];
array_push($my_array, $array_1);
array_push($my_array, $array_2);
array_push($my_array, $array_3);
array_push($my_array, $array_4);
print_r($my_array);
echo count($my_array) . "\n";
array_push($my_array, array_values($array_1));
array_push($my_array, array_values($array_2));
array_push($my_array, array_values($array_3));
array_push($my_array, array_values($array_4));
print_r($my_array);
echo count($my_array) . "\n";
?>
Link to the documentation
This is a list of the array functions we can find on the PHP documentation pages
https://www.php.net/manual/en/ref.array.php
I would encourage you to go and experiment and learn by doing because the Arrays are a big part of the PHP functionalities and can be bended as much as we want, we will see more in future on this.
Hope you enjoyed and if so, share and help us grow! Or Donate!
Share this content:
[…] Learning PHP – Arrays […]