Everybody uses arrays whether it's Php or not. Regarding Php arrays there are special functions you can use to make the most of them.
Arrays are easy to use and quite efficient.
What you probably already know
Arrays are data structures. This is what it looks like :
$alphabet = ["a", "b", "c"];
$_alphabet = range("a", "c");
It creates lists. Far far better than :
$a = "a";
$b = "b";
$c = "c";
... etc
And when dealing with specific values we can easily access them e.g :
$a = $alphabet[0];
$_a = reset($_alphabet);// it's just another way to get "a"
There are different types of arrays, some of them can have complex structure because specific keys can be defined as strings. You can even create multiple nested levels.
Php deeply
There are related terms for arrays :
- lists
- dictionaries
- collections
- trees
Php stores these structures in let's say "slots". Under the hood (internally) there is a hash function that computes data with integers in these slots.
Besides data are added in a way that Php remembers the exact ascending order they were added.
If you want to know how it's done you have to go deeper.
Ordered lists of elements
Simple arrays do not have named keys, if you want to access a particular value you can do the following :
$alphabet = ["a", "b", "c"];
$a = $alphabet[0];
In this case it works because obviously I have only 3 elements in that list so who cares... but in real life arrays can contain hundreds, thousands of entries. So that would be very lame this way.
Fortunately arrays can contain key/value pairs with named keys and when you specify names for your keys (as strings) the storing part is quite different.
Going a little bit deeper with hash tables
Ok we just saw data are stored in slots but that's still kind of abstract.
Arrays are actually implemented with hash tables. It's how Php indexes memory, it's how the internal hash function computes data with integers in slots.
When you use named keys (associative array), it's first converted to integers before indexing memory. The index stored as integer in memory contains the key/value pairs.
When you do this, you create maps.
Hash tables are used to store pretty much everything such as functions, methods, objects, arrays, variables.
C - origins
Php is implemented with C. The hash function computes your keys with integers. These integers are offsets in C arrays which contain linked list of possible values. Php runs all these possible values until it finds the matching element.
You don't have to specify key if it's an integer
Non-associative arrays do no need to be written like that :
$actresses = [ 0 => 'Scarlett Johansson', 1 => 'Ann Hathaway', 2 => 'Charlize Theron' ];
instead just write :
$actresses = [ 'Scarlett Johansson', 'Ann Hathaway', 'Charlize Theron' ];
Php automatically increments keys as integers starting at 0.
Array Destructuring
You can do the following :
$actresses = [
[ 'Scarlett Johansson', 'Lost in Translation' ],
[ 'Ann Hathaway', 'Interstellar' ],
[ 'Charlize Theron', 'Aeon Flux' ],
];
foreach ($actresses as $actress) {
[ $person, $movie ] = $actress;
echo "$person in $movie" . PHP_EOL;
}
Here [ $person, $movie ] = $actress;
is the shorthand for list($person, $movie) = $actress;
In this example if you just want movies without actresses indeed do not use $person
and add a comma before $movie
:
foreach ($actresses as $actress) {
[ ,$movie ] = $actress;
echo "$movie" . PHP_EOL;
}
Cut arrays in chunks
Php has a less known function called array_chunk()
which can divide your array into equal parts :
$actresses = [
[ 'Scarlett Johansson', 'Lost in Translation' ],
[ 'Ann Hathaway', 'Interstellar' ],
[ 'Charlize Theron', 'Aeon Flux' ],
];
print_r(array_chunk($actresses, 3));
will display :
Array
(
[0] => Array
(
[0] => Array
(
[0] => Scarlett Johansson
[1] => Lost in Translation
)
[1] => Array
(
[0] => Ann Hathaway
[1] => Interstellar
)
[2] => Array
(
[0] => Charlize Theron
[1] => Aeon Flux
)
)
)
Wrap up
Php arrays are very powerful and fun. You have to go a little bit deeper to understand how it's stored in memory. I hope this introduction encourages you to read more about it.