Sorting arrays by date

后端 未结 3 2116
梦毁少年i
梦毁少年i 2021-01-28 11:36

I\'ve got a question. I\'m building an array by getting data from mysql and merging three query results in one array.

I put data to array like this:

whil         


        
相关标签:
3条回答
  • 2021-01-28 12:18

    What you're trying to do is sort a multidimensional array, you can find plenty on Google about this. A nice elegant solution would be something like:

    // Sort the multidimensional array
    usort($results, "custom_sort");
    
    // Define the custom sort function
    function custom_sort($a,$b) {
         return $a['some_sub_var']>$b['some_sub_var'];
    }
    

    EDIT 1:

    For those in the comments doubting whether this code would work, please feel free to try it out (I even added in a date that's a duplicate for testing purposes):

    function custom_sort($a,$b) {
            return $a['added']>$b['added'];
    }
    
    $arrayToSort = array(
                        array(
                            "added" => "2012-01-17 07:33:53",
                            "type" => "1"
                        ),
                        array(
                            "added" => "2012-01-13 06:36:22",
                            "type" => "1"
                        ),
                        array(
                            "added" => "2012-01-09 04:01:12",
                            "type" => "2"
                        ),
                        array(
                            "added" => "2012-02-08 02:08:32",
                            "type" => "2"
                        ),
                        array(
                            "added" => "2012-01-25 00:09:08",
                            "type" => "2"
                        ),
                        array(
                            "added" => "2012-01-13 06:36:22",
                            "type" => "1"
                        ),
                        array(
                            "added" => "2012-01-13 06:36:22",
                            "type" => "1"
                        ),
                        array(
                            "added" => "2012-01-23 00:09:08",
                            "type" => "3"
                        ),
                        array(
                            "added" => "2012-01-22 00:09:08",
                            "type" => "3"
                        )
                    );
    usort($arrayToSort, "custom_sort");
    
    echo '<pre>';
    print_r($arrayToSort);
    echo '</pre>';
    

    A good place to test quickly would be to go to http://writecodeonline.com/php/.

    0 讨论(0)
  • 2021-01-28 12:25

    You probably should use UNION when selecting and not trying to sort arrays on your own. Anyway if you have to may use usort like this:

    function cmp( $a, $b){
      if( !($a instanceOf stdClass) && !($b instanceOf stdClass)){
        return 0;
      }
    
      // Check object
      if(  !($a instanceOf stdClass)){
        return -1;
      }
      if(  !($b instanceOf stdClass)){
        return 1;
      }
    
      $aVal = NULL;
      if( isset( $a->added)){
        $aVal = $a->added;
      } elseif( isset( $a->added_type_2)){
        $aVal = $a->added_type_2;
      } ...
    
      // The same for b
      if( ($aVal == NULL) && ($bVal == NULL)){
        return 0;
      }
    
      if( $aVal == NULL){
        return -1;
      }
    
      if( $bVal == NULL){
        return 1;
      }
    
      if( $aVal == $bVal){
        return 0;
      }
    
      return ($aVal > $bVal) ? 1 : -1;
    
    }
    
    usort( $array, 'cmp');
    

    As you may see the condition how to compare object may get complex when making sure they have correct types and correct values. When selecting mysql columns you should use at least SELECT added_type_2 AS added to have column names more compact and conditions simpler.

    0 讨论(0)
  • 2021-01-28 12:34

    You can use usort() if you change fetch object to fetch assoc:

    function my_date_sort($a,$b) {
       if(isset($a[0]) && isset($b[0])) {
           $a = strtotime($a[0]); // optionally convert to time
           $b = strtotime($b[0]);
           if($a == $b) return 0; 
           else return ($a > $b) ? 1 : -1;
       } else { // no valid subs, change this to put empty ones on top or bottom
           return 1; // put at bottom, -1 would put at top. 
    }
    
    
    usort($results, 'my_date_sort');
    

    Good luck...

    0 讨论(0)
提交回复
热议问题