4 Functions To Help You Master PHP Arrays & Data Manipulation

In almost any programming language, it’s almost impossible to find a useful script which does not make use of arrays to store data. Arrays are particularly important because they allow coders to group variables and store them under one central structure. In PHP, Arrays are simply ordered maps. In other words, each value is associated with a key that you can choose to specify when creating your Array.

Below, you will find 5 examples of problems we often come across when dealing with arrays.

I want to order a multi-dimensional array by a value…

Say you have the following:

$aMembers = array( array("name" => "Marc",
                         "date_joined" => "2011-02-04"),
                   array("name" => "Chris",
                         "date_joined" => "2011-02-03"));

I would like to order this array by the date joined. How exactly can I achieve this? Although there are definitely multiple ways to do this, the simplest way I’ve found to do this is by using the array_multisort function. To do this, we need to create a separate array which stores the field we want to use to order.

Here’s how:

$aDatesJoined = array();
foreach($aMembers as $Member) {
    $aDatesJoined[] = $Member['date_joined'];
}

So we now have another array made up of all the dates joined in the same order they are in the $aMembers array.

Finally, we can order the $aMembers array using the array_multisort function by passing the $aDatesJoined array as a parameter:

array_multisort($aDatesJoined, $aMembers);

The result? The members have now been sorted according to their date.

Learn more here.

I want to create a separate array that only includes members who joined on 2011-02-04

Again, let’s take the array above:

$aMembers = array( array("name" => "Marc",
                         "date_joined" => "2011-02-04"),
                   array("name" => "Chris",
                         "date_joined" => "2011-02-03"));

I want to extract an array out of that which includes members who joined on 2011-02-04. To do that, we will create a function that would accept one element of this array and return whether the member was added on that date or not:

function checkDate($Member)
{
    if ($Member['date_joined'] == "2011-02-04") return true;
    else return false;
}

With that in place, all we need to do now is the following:

$aNewArray = array_filter($aMembers, "checkDate"));

That function will run a filter through the Members array and return only the ones that return true. Easy, right?

Learn more here.

I want to change each member’s name to be uppercase…

Again, we’re going to start with the same array:

$aMembers = array( array("name" => "Marc",
                         "date_joined" => "2011-02-04"),
                   array("name" => "Chris",
                         "date_joined" => "2011-02-03"));

I would like to change all the names to be uppercase. Besides looping each item in the array and re-assigning the name, we can do it in a simpler way:

function uppercaseName(&$Member)
{
    $Member['name'] = strtoupper($Member['name']);
}

Note the we added to the parameter which basically passes the variable by reference. This allows the function to change the actual value, rather than create a copy of it. With that done, we simply need to run the following function:

array_walk($aMembers, 'uppercaseName');

And that’s that…

Learn more here.

I want to know on how many different dates members joined…

Again, we’re going to start with the same array:

$aMembers = array( array("name" => "Marc",
                         "date_joined" => "2011-02-04"),
                   array("name" => "Chris",
                         "date_joined" => "2011-02-03"));

I want to know how many different dates the array has. In this case, I know it will be just two but in the future, when my members array is much bigger, this might be a useful function to use.

The easiest way to do this is by creating an array with the dates joined, just like we did in the first example:

$aDatesJoined = array();
foreach($aMembers as $Member) {
    $aDatesJoined[] = $Member['date_joined'];
}

Now that we’ve built that array, we simply need to run the following line:

echo count(array_unique($aDatesJoined));

What the code above does is remove all duplicates and count the resulting array.

Learn more here.

A Summary

There are multiple ways to do the things we did above, including looping through the array and manipulating the data manually. However, it’s always a good idea to make use of PHP’s in-built array functions which allow you to re-use your code and achieve your objectives in less lines.

Any more tips? Are you aware of a better way to complete the tasks above? Let us know below!

Don't forget to check out more posts from the Lesson of the Week section.

blog comments powered by Disqus
.