Sort PHP arrays, save duplicates

I have an associative array with a date key and command value. For instance:

  • March 21, 2016 10:05 => "Detroit v. Philadelphia"
  • March 21, 2016 7:05 AM => 'Toronto vs. Ottawa'
  • March 21, 2016 7:05 AM => "Anachiem vs. Boston"
  • March 21, 2016 10:25 => "Chicago vs. Winnipeg"

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

I'm sure there is an easy way to handle this, but I'm going in circles.

Any help would be greatly 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;  
?>        
+4
source share
1 answer

, , , , , usort() , . :

<?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);

:

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
    )

)

, , strcmp() . cmp(), , php, .

+3

All Articles