Sorting arrays by date

2020-05-08 19:11发布

问题:

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:

while ($a = mysql_fetch_object($activities)) {
            $a->type = 1;
            $array1[] = $a;
        }
        while ($k = mysql_fetch_object($fuups)) {
        $k->type = 2;
            $array1[] = $k;
        }
        while ($x = mysql_fetch_object($refuups)) {
            $x->type = 3;
            $array1[] = $x;
        }

        return (object)$array1;

This returns something like this:

stdClass Object
(
    [0] => stdClass Object
        (
            [added] => 2012-01-17 07:33:53
            [type] => 1
        )

    [1] => stdClass Object
        (
            [added] => 2012-01-13 06:36:22
            [type] => 1
        )

    [2] => stdClass Object
        (
            [added_type_2] => 2012-01-09 04:01:12
            [type] => 2
        )

    [3] => stdClass Object
        (
            [added_type_2] => 2012-02-08 02:08:32
            [type] => 2
        )

    [4] => stdClass Object
        (
            [added_type_2] => 2012-01-25 00:09:08
            [type] => 2
        )

    [5] => stdClass Object
        (
            [added_type_3] => 2012-01-23 00:09:08
            [type] => 3
        )

    [6] => stdClass Object
        (
            [added_type_3] => 2012-01-22 00:09:08
            [type] => 3
        )

)

I tried things like asort, ksort, sort but no luck. also getting the dates with "order by added desc" thank you

回答1:

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...



回答2:

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/.



回答3:

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.



标签: php mysql arrays