What is an array, how to add values to it?
An array is a special type of variable, that can store multiple values at a time.
It can be very useful when we want to store in a single place, a list of similar values.
For example we may want to store a list of companies, without the PHP arrays we would need to have a new variable for each company.
<?php //creating a variable for each value $company_a = 'Tesla'; $company_b = 'Google'; $company_c = 'Nike'; $company_d = 'Amazon';
An easier and cleaner alternative for the code written above would be by using PHP arrays. To create an array we can either use the statement array() or the shorter version , they both do the same thing.
<?php //creating an array for all values $companies = array('Tesla', 'Google', 'Nike', 'Amazon');
You can create an array with its values already inside it or you can add them later.
<?php //creating an empty array $cars = ; //same as '$cars = array()' //adding values to the array $cars = 'Tesla'; $cars = 'BMW'; $cars = 'Toyota';
Based on array's structure, there are three types of PHP arrays:
- Indexed arrays - the arrays have a numeric index for each value;
- Associative arrays - the arrays have a string index for each value;
- Multidimensional arrays - the arrays have inside them one or more arrays.
Depending on how we want to store data and later access it back, we can choose to use a different type of array.
What are the indexed PHP arrays?
The indexed PHP arrays store each value paired with a numeric key, named index.
By default, PHP will assigned an index to each value (if we don't assign manually an index) in a linear fashion (like 0, 1, 2, 3 ...). It's important to remember that automatically the system gives to the first value the index '0' (doesn't starts with '1').
To access a specific value we can simply specify the index of the value we want retrieved, like in the example bellow:
<?php // PHP will assign automatically an index(starting with 0) to each value $coffee = ['Long Black', 'Cappuccino', 'Espresso', 'Macchiato', 'Mochaccino']; // using print_r PHP will output the values of the array print_r($coffee); /* PHP output: Array (  => Long Black  => Cappuccino  => Espresso  => Macchiato  => Mochaccino ) */ // output a specific value from the array echo 'A very popular coffee is '.$coffee.'.'; // will output 'A very popular coffee is Cappuccino.' // assign the value to a variable $selected_coffee = $coffee;
We can also assign manually an index to each value, when we add it to the array, like in the example bellow.
<?php /* we assigned manually the index values, even though we assigned the values in a random way it would make it easier to retrieve the values if they in a ordered way */ $main_coffee_types = [4 => 'Long Black', 7 => 'Cappuccino', 8 => 'Espresso', 11 => 'Mochaccino'] // assigning the values one by one $types_of_cappuccino = 'Traditional Cappuccino'; $types_of_cappuccino = 'Iced Cappuccino'; $types_of_cappuccino = 'Dry Cappuccino'; $types_of_cappuccino = 'Wet Cappuccino';
What are the associative PHP arrays? Difference between an indexed array and an associative array
The associative PHP arrays are similar to the indexed arrays. The difference between an indexed array and an associative array is that instead of using numeric keys for each value, we need to use string keys (named keys) for each value. We will need to specify the string key of each value, they are not automatically assigned as the numeric keys are in the indexed PHP arrays.
We can create an associative array as in the examples bellow:
- by declaring an array with the values already inside it
<?php $food_calories = ['bread' => 265, 'fries' => 312, 'apple' => 52, 'yogurt' => 59];
- by adding the values later
<?php $food_colour = ; $food_colour['strawberries'] = 'red'; $food_colour['bananas'] = 'yellow'; $food_colour['watermelons'] = 'green';
We then can later access the values of the array by specifing the key of the value we want, as in the example.
<?php //the bottom example will output for $food_colour['strawberries'] the value 'red' echo 'The strawberries are '.$food_colour['strawberries'].'.';
What are the multidimensional PHP arrays?
We also have PHP arrays that store other arrays inside them. This type of arrays are called multidimensional arrays.
For example, we may have a list of items that have more properties we want to store, like in the example bellow where we have a list of companies.
|Tesla||automotive, energy storage||2003|
|The Boring Company||geotechnical engineering||2016|
To keep the data of the above list in a variable we can make a two dimensional array(a multidimensional array with 2 level depth). First we will make an array, and then inside the array we will put another array for each company containing its information.
<?php //array containing arrays with companies info $companies = [ ['Tesla', 'automotive, energy storage', 2003], ['SpaceX', 'aerospace', 2002], ['Neuralink', 'brain-computer interface', 2016], ['The Boring Company', 'geotechnical engineering', 2016], ['OpenAI', 'artificial intelligence', 2015] ];
In the example above the '$companies' array has a 2 level depth. An multidimensional array can be three, four, five or more deep, but you should not try to make it too deep as it will become harder for people to manage it.
We can easily retrieve the data from the array by using indexes. The first array in the list, containing the info for the Tesla company, has the index 0. To access that array we'll use '$companies'. Now, in the array containing the Tesla's info the name of the company has the index 0 ('$companies'), the industry has the index 1 ('$companies') and the founded year has the index 2.
<?php //get Tesla info echo 'The '.$companies.' company, founded in '.$companies.', is in the '.$companies.' industry'.; /* this will output: The Tesla company, founded in 2003, is in the automotive, energy storage industry. */ //get SpaceX info echo 'The '.$companies.' company, founded in '.$companies.', is in the '.$companies.' industry'.; /* this will output: The SpaceX company, founded in 2002, is in the space industry. */
How you create a new empty array?
What is the index of the value 'Ginger', from the array bellow?
<?php $spices = array('Basil', 'Ginger', 'Turmeric');
What type of data is used for the keys of an associative array?