Jump to content


Photo

array Order


  • Please log in to reply
1 reply to this topic

#1 wolves

wolves
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 03 April 2006 - 05:27 PM

I have an array like this


Array ( [0] => Array ( [id] => 99 [text] => ZZZZZ ) [1] => Array ( [id] => 89 [text] =>  MYTEST ) [2] => Array ( [id] => 79 [text] => AAAAA ) [3] => Array ( [id] => [text] => SELECT ONE ) )


Now, how to order this array by alphabetic [text] key, and keeping the [id] intact?

tks..


arsort , solved

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 03 April 2006 - 07:58 PM

arsort() will sort it by id descending.

If that solved it, you asked the wrong question.

But if you do want it sorted by 'text' you need usort() and a custom sort function.

$data = array (
        0 => array (
               'id' => 99,
               'text' => 'ZZZZZ'
               ) ,

        1 => array (
               'id' => 89,
               'text' => 'MYTEST'
               ),

        2 => array (
               'id' => 79,
               'text' => 'AAAAA'
               ),

        3 => array (
               'id' => 109,
               'text' => 'SELECT ONE'
               )
        );

function textsort($a, $b) {
         return strcmp($a['text'], $b['text']);
}

usort ($data, 'textsort');

// check results
echo '<pre>', print_r ($data, true), '</pre>';

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users