Francesca
Francesca

Reputation: 28148

Dynamically create a multidimensional array in date order

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

Answers (2)

Elias Van Ootegem
Elias Van Ootegem

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:

  • Sanitize data: You have an empty string (' ') 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, too
  • Then, assign each chunk of data to the corresponding keys
  • Compare all the dates and sort accordingly

First 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
});

live demo here

Upvotes: 1

jogesh_pi
jogesh_pi

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

Related Questions