Below, you will find 5 examples of problems we often come across when dealing with arrays.
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:
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:
The result? The members have now been sorted according to their date.
Learn more here.
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:
With that in place, all we need to do now is the following:
That function will run a filter through the Members array and return only the ones that return true. Easy, right?
Learn more here.
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:
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:
And that’s that…
Learn more here.
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:
Now that we’ve built that array, we simply need to run the following line:
What the code above does is remove all duplicates and count the resulting array.
Learn more here.
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" )); |
Here’s how:
1
2
3
4
| $aDatesJoined = array (); foreach ( $aMembers as $Member ) { $aDatesJoined [] = $Member [ 'date_joined' ]; } |
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 ); |
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" )); |
1
2
3
4
5
| function checkDate($Member) { if ($Member[ 'date_joined' ] == "2011-02-04" ) return true ; else return false ; } |
1
| $aNewArray = array_filter ( $aMembers , "checkDate" )); |
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" )); |
1
2
3
4
| function uppercaseName(& $Member ) { $Member [ 'name' ] = strtoupper ( $Member [ 'name' ]); } |
1
| array_walk ( $aMembers , 'uppercaseName' ); |
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" )); |
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' ]; } |
1
| echo count ( array_unique ( $aDatesJoined )); |
Learn more here.
0 comments:
Post a Comment