sorting multidimensianal array based on the date field

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pradeepjain
    Contributor
    • Jul 2007
    • 563

    sorting multidimensianal array based on the date field

    Hello Guys,
    I am using a multidimensiona l array in which there is a date field also..i want to sort the multidimensiona l array based on this date filed how to do this.



    Thanks,
    Pradeep
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    In what level is the date field? In the top array? or nested array?

    that would help.

    see array_sort() in PHP docs too!

    Cheers,


    Dan

    Comment

    • pradeepjain
      Contributor
      • Jul 2007
      • 563

      #3
      my array is in this format
      $tmp3 = array(
      'Count' => $i+1,
      'Description' => $sub_row['Description'],
      'Posted' => $sub_row['2'],
      'Expires' => $sub_row['3'],
      'Type' => $sub_row['Type'],
      'ID' => $sub_row['ID'],
      'Doc' => $sub_row['Doc'],
      'Doc1' => $sub_row['Doc1'],
      'Doc2' => $sub_row['Doc2'],
      'Name' => $sub_row['Name'],
      'Name1' => $sub_row['Name1'],
      'Name2' => $sub_row['Name2']


      );
      where Posted is in Date format ...i need to sort this array based on the Posted value ...how to do this


      Cheers,
      Pradeep

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        There's no such array_sort() function, unfortunately. asort().

        Comment

        • dlite922
          Recognized Expert Top Contributor
          • Dec 2007
          • 1586

          #5
          Originally posted by pradeepjain
          my array is in this format
          $tmp3 = array(
          'Count' => $i+1,
          'Description' => $sub_row['Description'],
          'Posted' => $sub_row['2'],
          'Expires' => $sub_row['3'],
          'Type' => $sub_row['Type'],
          'ID' => $sub_row['ID'],
          'Doc' => $sub_row['Doc'],
          'Doc1' => $sub_row['Doc1'],
          'Doc2' => $sub_row['Doc2'],
          'Name' => $sub_row['Name'],
          'Name1' => $sub_row['Name1'],
          'Name2' => $sub_row['Name2']


          );
          where Posted is in Date format ...i need to sort this array based on the Posted value ...how to do this


          Cheers,
          Pradeep
          okay fine array_sort() doesn't exist, but i know this exists!

          PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


          That should be what your looking for. Let us know if you don't know how to use it, or can't get it to work.

          Cheers,


          Dan

          Comment

          • dlite922
            Recognized Expert Top Contributor
            • Dec 2007
            • 1586

            #6
            Originally posted by dlite922
            okay fine array_sort() doesn't exist, but i know this exists!

            PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


            That should be what your looking for. Let us know if you don't know how to use it, or can't get it to work.

            Cheers,


            Dan
            Okay i'll be nice, here's an example:

            [PHP] <?php
            // sample array:
            $data = array(
            array('date' => '2008-06-07','name'=>'na me4'),
            array('date' => '2008-06-11','name'=>'na me2'),
            array('date' => '2009-05-30','name'=>'na me5'),
            array('date' => '2008-02-02','name'=>'na me1'),
            array('date' => '2007-01-11','name'=>'na me3'),
            );

            // Obtain a list of columns
            foreach ($data as $key => $row) {
            $date[$key] = $row['date'];
            }
            // Sort the data by $date
            // Add $data as the last parameter,
            array_multisort ($date, $data);
            print_r($data);
            ?>

            [/PHP]

            you can even do a secondary sort by the name. For example if you have two dates the same, in what order do you want 'name' to be in.

            Cheers,


            Dan

            Comment

            • pradeepjain
              Contributor
              • Jul 2007
              • 563

              #7
              [PHP]function record_sort($re cords, $field, $reverse=false)
              {
              $hash = array();

              foreach($record s as $record)
              {
              $hash[$record[$field]] = $record;
              }

              ($reverse)? krsort($hash) : ksort($hash);

              $records = array();

              foreach($hash as $record)
              {
              $records []= $record;
              }

              return $records;
              }
              $coll = record_sort($co ll, "name");
              [/PHP]

              i was using this to do sorting everytime and only for the date format its not working .....
              can we modify it..

              Thanks,
              Pradeep

              Comment

              • pradeepjain
                Contributor
                • Jul 2007
                • 563

                #8
                srry guys the script tht i gave u is working fine,....i had only made a small mistake....than ks for all ur help


                Thanks,
                Pradeep

                Comment

                Working...