Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting PHP arrays, keeping duplicates

Tags:

arrays

php

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;  
?>        
like image 580
xar86413 Avatar asked Mar 22 '16 01:03

xar86413


1 Answers

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.

like image 91
gabe. Avatar answered Nov 07 '22 03:11

gabe.