Sort arrays by date

I have a question. I create an array by retrieving data from mysql and combining the three query results in one array.

I put the data into an array as follows:

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 dates with "order by adding desc" thanks

0
source share
3 answers

You can use usort () if you modify the fetch object to retrieve the link:

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
source

What you are trying to do is sort a multidimensional array, you can find a lot about Google about it. A good 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:

, , , , ( , ):

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>';

http://writecodeonline.com/php/.

+2

, UNION . , usort, :

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

, , , . mysql SELECT added_type_2 AS added, .

0

Source: https://habr.com/ru/post/1660053/


All Articles