5

I have an associative array with the key of date, and a value of teams. For instance:

  • March 21, 2016 10:05 => 'Detroit vs. Philly'
  • March 21, 2016 7:05 =>'Toronto vs. Ottawa'
  • March 21, 2016 7:05 => 'Anahiem vs. Boston'
  • March 21, 2016 10:25 => 'Chicago vs. Winnipeg'

The problem is the RSS feed that I am parsing does not give me this data in an ordered fashion. So I need to order these games by the date, and when I add these fields in an associative array, duplicate dates (you can see that two games start at 7:05 on March 21st) are omitted because two keys can not be the same. I have tried to reverse the data, so that the key is the value and the value is the key and I can sort it this way, but when flip the array back, (array_flip($input);) the same problem occurs because again two keys cannot be the same.

I'm sure there is a simple way to handle this, but I'm going around in circles.

Any help would be very much appreciated.

<?php
      foreach ($feed->get_items() as $item): // this is my feed parser 
            $string = $item->get_title();    // gets each element
            preg_match_all('/\((.*?)\)/', $string, $out); 
            $timedate = ($out[1][2]);
            $array[$timedate] = $string; // creates an array with date as key, string data as values
          endforeach;  
?>        
3
  • the date and time are the keys?, do note that keys are supposed to be unique, March 21, 2016 7:05 occurs twice Commented Mar 22, 2016 at 2:08
  • 1
    Without the code is hard to help you. Basically, you have to add an incrementing suffix to keys or to use a enumerated multidimensional array. Commented Mar 22, 2016 at 2:08
  • Use usort() for this. Using keys for this makes no sense. Commented Mar 22, 2016 at 2:14

1 Answer 1

5

To do what you want, you will have to put the data you have into a slightly more complex array, and then use the usort() function to sort it based on the key you would like to sort it on. Here is an example:

<?php                                                                                                                                                                                                                                       

// Multidimensional array of 'games'                                                                                                                                                                
$games[] = array('date' => 'March 21, 2016 10:05',                              
               'title' => 'Detroit vs Philly');                                 

$games[] = array('date' => 'March 21, 2016 7:05',                               
               'title' => 'Toronto vs Ottawa');                                 
$games[] = array('date' => 'March 21, 2016 7:05',                               
               'title' => 'Anaheim vs Boston');                                 
$games[] = array('date' => 'March 21, 2016 10:25',                              
               'title' => 'Chicago vs Winnipeg');                               

// Define a custom sort function to sort based on
//  the date index.  This will not sort properly
//  since I'm only using strcmp, but it works as 
//  an illustration.  For more details see: 
//  http://php.net/manual/en/function.usort.php
function cmp($a, $b)                                                            
{                                                                               
    return strcmp($a['date'], $b['date']);                                      
}                                                                            

// Sort the array
usort($games, "cmp");                                                           

print_r($games);

This will produce the following sorted array:

Array
(
[0] => Array
    (
        [date] => March 21, 2016 10:05
        [title] => Detroit vs Philly
    )

[1] => Array
    (
        [date] => March 21, 2016 10:25
        [title] => Chicago vs Winnipeg
    )

[2] => Array
    (
        [date] => March 21, 2016 7:05
        [title] => Toronto vs Ottawa
    )

[3] => Array
    (
        [date] => March 21, 2016 7:05
        [title] => Anaheim vs Boston
    )

)

You will notice that the dates aren't exactly properly sorted since strcmp() is just doing a basic string comparison. You could add more functionality to the cmp() function we defined to convert the strings to php dates, and then do actual date comparisons on them instead.

Sign up to request clarification or add additional context in comments.

1 Comment

That was exactly what I was looking for. Thank you! @gabe

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.