PHP arrays allow us to store multiple values under a single variable name. This means that instead of needing to define a variable for each item, you can store them all together using a single variable.
Arrays are populated in key-value pairs. You have to provide what you want to store in the array, the value, and then a unique identifier, or ID, for the item, called the key. Providing back the ID is how you can access the value you stored in the array.
There are three different kinds of arrays that you can create in PHP:
Numeric Arrays
Associative Arrays
Multidimensional Arrays
Let's learn about the differences between these types of arrays.
Numeric Arrays
Numeric arrays are arrays that use a number as the key. That is, when you want to store an item in the array, give it a number as the key and it will be stored there. Keep in mind that the first element in the array has an index of 0, not 1.
Associative arrays are very similar to numeric arrays except that instead of the keys being numbers, the keys are simply another value of some kind. This means that the value can be, for example, a string.
Sometimes you won't actually know if the associative array contains the key you want to access. If you try to access it anyways, you'll get an error. To avoid this, check for the existence of the key using the isset() function:
PHP
<?php$person = 'Jasmine';
$schools = ['Bob' => 'NYU', 'Jack' => 'Columbia', 'Jasmine' => 'Yale'];
if (isset($schools[$person])) {
echo($person . ' attends ' . $schools[$person]);
} else {
// the key does not exist
}
?>
HTML
Jasmine attends Yale
The isset() function can be used on anything, not just for checking keys in an associative arrays. You can also check for the existence of variables in general that may have been defined in other files. The function will return a boolean indicating whether or not the variable exists and has a value.
Multidimensional Arrays
Multidimensional arrays is an array in which each element is an array itself. Following our earlier example, we can store an entire array of information, instead of just the name of the school they attend.
Sorting arrays in PHP can be complicated given how many functions there are for it, but once you find the one you need, it gets the job done. Here are just some of the sorting functions available to you:
sort(): Sorts a numeric array by key, low to high.
rsort(): Sorts a numeric array by key, high to low.
array_multisort(): Sorts multi-dimensional arrays by value.
asort(): Sorts associative arrays by value, low to high.
arsort(): Sorts associative arrays by value, high to low.
The first parameter is the array you want to slice. The second parameter is the index you want to begin at. The third parameter is the number of elements you want returned back, or the new length.
array_splice
You can replace selected elements of an array with another array using array_splice().
Array ( [0] => amazing [1] => bacon [c] => car [d] => dark )
explode
Use the explode() function to take a string and divide it up into an array of elements. Let's say you wanted to break up a text by a comma, also known as the delimiter:
Array ( [d] => dark [c] => car [b] => book [a] => apple )
array_map
Use array_map when you want to apply a function to every element in an array to return a new array with them. For example, let's say you wanted to double every number in an array:
Use the array_filter function to keep or remove elements from an array depending on whether or not that element fulfills a condition you define. Let's say you only want odd numbers:
Your function returns true if the element should remain or false if the element should be filtered out.
array_reduce
The array_reduce function is useful for when you want to perform a single operation across an entire array and return a single end result. A perfect example is taking a sum of an array of numbers:
The way this function works is rather simple. The $carry variable carries over the value of the all the previous iterations. So when we call $carry += $number;, we are adding the current number's value, then returning it to be used in the next element's iteration.