Thursday 15 May 2014

mysql - Sorting in php Arrayss -


My SQL table contains IDs, book names, styles and ratings. Currently my array looks like this in my SQL table (Output Array ([book] => Array ([0] = & gt; array ([0] => 1 [1] => one Book [2] = & gt; Fictional / Horror [4] => 8.9) [1] => Array ([0] => 2 [1] => two book [2] = Comedy [4] => 8.3)))

I want to sort this array from the title to the DESC with the title in the future. The array variable $ The book is so why did i try to do ksort ($ book) also arsort for some reason? Thank you!

If you are enabled: Instead Use SQL Sorting, because only use ARA-functionality of speed and flexibility (use DB engine where it is an appapriate) if you really need it!

Or Select ID, Book name, Style, / P>

If you still have to use array sorting , Use usort in PHP:

  & lt;? Php // book array $ bookAre ['book'] [0] = array (2, 'book one', 'fictional / horror', 8.9); $ Booker ['book'] [1] = array (1, 'book two', 'comedy', 8.3); Function sortByIndex ($ a, $ b) {return $ a [0] - $ b [0]; } Usort ($ bookArray ['book'], 'Sortboy Index'); Echo 'RESULT =' print_r ($ bookArray, true); ? & Gt; Results in this output: RESULT = Array ([book] => Array ([0] => Array ([0] => 1 [1] => Book two [2] => ; Comedy [3] => 8.3) [1] => Array ([0] => 2 [1] => a book [2] => Fiction / Horror [3] = & Gt; 8.9)))  

The book comes here first, because in my example I created the first index with the first 'book two' value.

No comments:

Post a Comment