This week in our continuing series on PHP 101 we will explore some useful functions for working with arrays. Arrays are an incredibly useful data structure for storing multiple pieces of information. They allow for easy and relatively quick recall of information by referencing an item’s index. There are upsides and downsides to every data structure and one should use the one that is best for their case. A discussion of the benefits and drawbacks to each is beyond the scope of this post, but there is a lot of great material out there on the topic, including the fantastic BaseCS podcast.
Let’s create the following array:
$myNumbers = [2,3,4,5,6];
We now have a very simple array holding the values of 2, 3, 4, 5 and 6. This array can be accessed through the $myNumbers
variable. What if we wanted to create a new variable that held the result of multiplying each number by 2? What would be the quickest way of accomplishing that? Welcome to the array_map()
function in PHP. This function maps over the array applying a callback function to each value in the array. This is what it looks like in action:
$myNumbers = [2,3,4,5,6];
function multiplyThem($num) {
return ($num * 2);
}
$multiplied = array_map("multiplyThem", $myNumbers);
print_r($multiplied);
In the example above we begin again with our variable $myNumbers
holding our original array. At this point we want to create our function, which will act as our callback in array_map()
. We create a function called multiplyThem
that simply takes in an argument and multiplies it by 2 and returns the new value. We then create a new variable called $multiplied
to hold the result of running array_map()
with two arguments: the callback function and the array to perform the function on. Lastly, we print out the data in our new variable using print_r()
. The end result is a variable that now holds an array of values 4, 6, 8, 10 and 12.
Another useful function to work with arrays is array_filter()
, which will filter the values of an array as specified in the callback function. Interestingly, if no callback function is provided when using array_filter()
then any item that returns FALSE
will be removed. Let’s say we wanted to filter our $myNumbers
array for only even numbers. Let’s take a look at an example provided in the PHP Manual:
function even($var) {
return($var % 2 == 0);
}
$evenNumbers = array_filter($myNumbers, "even");
print_r($evenNumbers);
In the above example we create a function called even
, which returns any value that is divisible by 2, i.e. any even numbers. We then supply that function as the callback for array_filter()
along with the array we want to filter. Lastly, we print out the information of our new variable $evenNumbers
, which holds the filtered array and the result, in our case, will be an array of 2, 4 and 6.
The last function we’ll look at today in working with arrays is array_reduce()
, which as it’s name sounds like, reduces the values of an array into a single value. This function, like the previous ones, also taken in a callback function. It makes use of two variables: One variable to hold the accumulated result of the reduce action and the other variable to store the item it is currently acting on. So if we wanted to add all of our array values together we could use it like this:
function sum($accum, $item) {
$accum += $item;
return $accum;
}
$mySum = array_reduce($myNumbers, "sum");
print_r($mySum);
In the above example we create a function called sum
that takes in two arguments: the accumulated total and the iterated item. The item is added to the total of $accum
and this repeats for all the items in the array. The total value for $accum
is then returned. We then create a new variable, $mySum
, to hold the value of running array_reduce()
with two arguments: the original array and the callback function we just created. The new variable $mySum
will, once it is all done, hold the value of 20.
There is a lot more built in functionality within PHP to work with arrays and I encourage you visit the PHP Manual to discover them, including array_unique()
, array_count_values()
and much more. Happy coding!
Top comments (0)