Jump to content

[SOLVED] Sort array by date


mysterbx

Recommended Posts

Hello,

 

I need to somehow sort array by date...

Yesterday I tried that, took me 2 hours, and didnt find anything that works... i even found 3 topics here, on phpfreaks, they also did not work...

 

Here is my array (example, the real one has 10k records in it):

Array
(
    [0] => Array
        (
            [0] => 10-27-2004|Site NR1|http://www.rapidshare.com/|Rapidshare|x1|c1
            [1] => 10-08-2008|Site NR2|http://www.google.com/|Google|x2|c2
        )

    [1] => Array
        (
            [0] => 10-27-2004
            [1] => 10-08-2008
        )

    [2] => Array
        (
            [0] => Site NR1
            [1] => Site NR2
        )

    [3] => Array
        (
            [0] => http://www.rapidshare.com/
            [1] => http://www.google.com/
        )

    [4] => Array
        (
            [0] => Rapidshare
            [1] => Google
        )

    [5] => Array
        (
            [0] => x1
            [1] => x2
        )

    [6] => Array
        (
            [0] => c1
            [1] => c2
        )

)

 

the latest entries should be at the top.

i tried some codes yesterday  (usort,rsort) they change the places of arrays Array[1] is Array[6] and so on...

 

it would be soo great if someone would make this work...

 

THANKS!

Link to comment
Share on other sites

Hello ,

 

I solved a somewhat similar problem with uasort. [maintain index association unlike asort]

 

in ur case, the data in the array is not always date.. sometimes its other information instead of date, and sometimes a combination of date and other information .

 

So I guess you will have to write conditions for detecting that too , and then sort, into the "user defined function " that you are supplying uasort with

 

Also make sure u understand how uasort or usort works clearly.. because that usually takes some time and can be a bit confusing .

 

Hopefully

Rakesh ::)

Link to comment
Share on other sites

already done:

function sd($a, $b)
{
list($x, $y, $z) = explode("-", $a);
$a = "$z-$x-$y";
list($x, $y, $z) = explode("-", $b);
$b = "$z-$x-$y";
return -strcmp($a, $b);
}

 

well lets say the array is name $array

i tried usort($array[1],'sd');, this only sorts the array[1], other arrays dont change

i tried usort($array,'sd');, this mixes up all the array, the places change...

 

what am i doing wrong?

Link to comment
Share on other sites

I found an example:

$ar = array (
           array ('2007-05-10', '2007-06-13', '2007-04-10'),
           array ('kiwi', 'pear', 'apple'),
           array ('car', 'bus', 'plane')
        );

 

array should be sorted by the date.

 

LIKE:

2007-06-13>>>pear>>>bus

2007-05-10>>>kiwi>>>car

2007-04-10>>apple>>>plane

 

please help me out here!

Link to comment
Share on other sites

alright, i'm going to use your most recent array example.  the first thing to do is to switch the dates into timestamps using strtotime().  you can then sort numerically in descending order, and use the keys from this array to grab other info:

 

<?php
$ar = array (
           array ('2007-05-10', '2007-06-13', '2007-04-10'),
           array ('kiwi', 'pear', 'apple'),
           array ('car', 'bus', 'plane')
        );

// nab the dates in timestamp format
$dates = array_map('strtotime', $ar[0]);

// sort descending numerically
arsort($dates, SORT_NUMERIC);

// traverse the new dates array
foreach ($dates AS $k => $val)
{
  echo $ar[0][$k].'>>>'.$ar[1][$k].'>>>'.$ar[2][$k].'<br />';
}
?>

 

the output may not be exactly what you're after, but hopefully this gives you enough information to edit it to your liking.

Link to comment
Share on other sites

you could create a custom function that converts it, using mktime():

 

function custom_mktime($date)
{
  // explode the date
  list($day, $month, $year) = explode('/', $date);
  // pass back a timestamp
  return mktime(0, 0, 0, $month, $day, $year);
}

$dates = array_map('custom_mktime', $ar[0]);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.