Wednesday, 19 December 2012

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:
1
2
3
4
$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:
1
2
3
4
$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:
1
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:
1
2
3
4
$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:
1
2
3
4
5
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:
1
$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:
1
2
3
4
$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:
1
2
3
4
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:
1
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:
1
2
3
4
$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:
1
2
3
4
$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:
1
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.

0 comments:

Post a Comment

Subscribe to RSS Feed Follow me on Twitter!