Reputation: 28148
I'm trying to work out a way to create a multidimensional array from a previous array, and then sort it into date order.
Presently my array looks like this:
array(5) {
[0]=> string(161) "2013-09-18~Ready For Retina HD: Create Pixel-Perfect Assets For Multiple Scale Factors~ready-for-retina-hd-create-pixel-perfect-assets-for-multiple-scale-factors"
[1]=> string(93) " 2010-10-20~Taking A Closer Look At Tech Conferences~taking-a-closer-look-at-tech-conferences"
[2]=> string(71) " 2014-10-19~Wayfinding For The Mobile Web~wayfinding-for-the-mobile-web" [3]=> string(60) " 2014-05-15~Freebie: Icons Of Autumn~freebie-icons-of-autumn"
[4]=> string(1) " "
}
Each array element now needs to be further broken down by using the delimiter ~
, I had previously done this using a foreach loop however this created 3 different arrays of data, not one multidimensional array.
I obviously need to name each of the values, $date
, $title
and $filename
.
How would you do this with a foreach loop so that it stores it in one multi-dimensional array? This is what I had previously
foreach ($masterpostlist as &$post){
$post = explode('~',$post);
}
How would you then sort the date part of the array so that they order newest first?
Upvotes: 0
Views: 334
Reputation: 76413
There's a number of ways you can do this, but I think the shortest (in terms of code to write) I can think of is to use array_map
and usort
.
First, let's establish what we have to do:
' '
) in your array, we need to get rid of that and some of your strings contain leading spaces, so we'll have to get rid of that, tooFirst things first, let's sanitize the data:
//assume $a is your array:
$a = array_filter(//gets rid of empty keys
array_map('trim', $a)//trim each element in $a
);
Next, let's create a 2D array, where each index contains an assoc array with the keys date, title and filename:
$x = array_map(
function($value)
{
return array_combine(
['date', 'title', 'filename'],//the keys
explode('~', $value)//the values
);
}, $a);
Then lastly, let's sort the lot:
usort($x, function($a, $b)
{
$dateA = new DateTime($a['date']);//create a DateTime object from value
$dateB = new DateTime($b['date']);
if ($dateA == $dateB)//if both dates are the same
return 0;//don't move
return $dateA < $dateB ? 1 : -1;//descending order
});
Upvotes: 1
Reputation: 9782
Take a look on this example:
$arr = array("2013-09-18~Ready For Retina HD: Create Pixel-Perfect Assets For Multiple Scale Factors~ready-for-retina-hd-create-pixel-perfect-assets-for-multiple-scale-factors", "2013-09-20~Ready For Retina HD: Create Pixel-Perfect Assets For Multiple Scale Factors~ready-for-retina-hd-create-pixel-perfect-assets-for-multiple-scale-factors", "2013-09-15~Ready For Retina HD: Create Pixel-Perfect Assets For Multiple Scale Factors~ready-for-retina-hd-create-pixel-perfect-assets-for-multiple-scale-factors");
$new = array();
foreach($arr as $v) {
$ex = explode("~", $v);
$new[] = array('date'=>$ex[0], 'title'=>$ex[1], 'detail'=>$ex[2]);
}
print_r($new);
// Newest Date First
function sortByOrder($a, $b) {
return strtotime($b['date']) - strtotime($a['date']);
}
usort($new, 'sortByOrder');
print_r( $new );
Hope this is what you want; DEMO
Upvotes: 0