SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Sorting an Array based on a Key

    Heya,

    I am trying to sort these array based on the date..

    1: I first merge them, so it's one big array.
    2: Then how do I sort them based each items date?

    PHP Code:
    $sample[0]['item']['text'] = 'sample1';
    $sample[0]['item']['date'] = '1299781938';

    $sample[1]['item']['text'] = 'sample2';
    $sample[1]['item']['date'] = '1299781932';

    $sample[2]['item']['text'] = 'sample3';
    $sample[2]['item']['date'] = '1199781936';

    $sample[3]['item']['text'] = 'sample4';
    $sample[3]['item']['date'] = '1199782936';


    $other[0]['item']['text'] = 'other1';
    $other[0]['item']['date'] = '1211181938';

    $other[1]['item']['text'] = 'other2';
    $other[1]['item']['date'] = '1204221932';

    $other[2]['item']['text'] = 'other3';
    $other[2]['item']['date'] = '1119781936';

    $other[3]['item']['text'] = 'other4';
    $other[3]['item']['date'] = '1123782936';


    $new array_merge($sample$other);

    // I know below won't work, but Im trying do imitate something like this:
    // array_mutli_sort($new, $new[]['item']['date']); 

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,036
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    PHP Code:
    $sample[0]['item']['text'] = 'sample1';
    $sample[0]['item']['date'] = '1299781938';

    $sample[1]['item']['text'] = 'sample2';
    $sample[1]['item']['date'] = '1299781932';

    $sample[2]['item']['text'] = 'sample3';
    $sample[2]['item']['date'] = '1199781936';

    $sample[3]['item']['text'] = 'sample4';
    $sample[3]['item']['date'] = '1199782936';


    $other[0]['item']['text'] = 'other1';
    $other[0]['item']['date'] = '1211181938';

    $other[1]['item']['text'] = 'other2';
    $other[1]['item']['date'] = '1204221932';

    $other[2]['item']['text'] = 'other3';
    $other[2]['item']['date'] = '1119781936';

    $other[3]['item']['text'] = 'other4';
    $other[3]['item']['date'] = '1123782936';


    $new array_merge($sample$other);

    // var_dump($new);

    function mySort($item1$item2)
    {
       
    $d1=$item1['item']['date'];
       
    $d2=$item2['item']['date'];
       if (
    $d1===$d) return 0;
       return 
    $d1<$d2 ? -1;
    }

    usort($new'mySort');

    var_dump($new); 
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    damn, scallioxtx beat me
    PHP Code:
    $sample[0]['item']['text'] = 'sample1';
    $sample[0]['item']['date'] = '10';

    $sample[1]['item']['text'] = 'sample2';
    $sample[1]['item']['date'] = '4';

    $sample[2]['item']['text'] = 'sample3';
    $sample[2]['item']['date'] = '7';

    $sample[3]['item']['text'] = 'sample4';
    $sample[3]['item']['date'] = '2';


    $other[0]['item']['text'] = 'other1';
    $other[0]['item']['date'] = '1';

    $other[1]['item']['text'] = 'other2';
    $other[1]['item']['date'] = '5';

    $other[2]['item']['text'] = 'other3';
    $other[2]['item']['date'] = '6';

    $other[3]['item']['text'] = 'other4';
    $other[3]['item']['date'] = '4';


    $new array_merge($sample$other);

    foreach (
    $new as $key => $row) {
        
    $date[$key]  = $row['item']['date'];
    }

    array_multisort($dateSORT_DESC$new); 
    aaron-fisher.com - PHP articles and more

  4. #4
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys i see how its done now!!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •