Usually it will not be necessary to write your own sort functions. PHP offers several functions for sorting arrays. The most basic is sort.
The sort function puts all the elements in the array in order from lowest to highest. If the array contains any strings, then this means ordering them by the ASCII codes of each character. If the array contains only numbers, then they are ordered by their values. The indices—the values used to reference the elements—are discarded and replaced with integers starting with zero. This is an important effect. Notice that, although I use some numbers and a string to index the array, after I sort it, all the elements are numbered zero through four. Keep this in mind if you ever need to clean up the indices of an array.
Another point worth noting in Listing 15.3 is the order of the output: Apple, Blueberry, Watermelon, apple, pear. A dictionary might list apple just before or just after Apple, but the ASCII code for A is 65. The ASCII code for a is 97.
The rsort function works exactly like sort, except that it orders elements in the reverse order. Try modifying the code by changing sort to rsort.
Two other two sort functions, asort and arsort, work in a slightly different way. They preserve the relationship between the index and the element. This is most useful when you have an associative array. If the array is indexed by numbers, you probably do not want to preserve their indices. On the other hand, what if you did? illustrates a possible scenario.
Using the sort Function
Here I've used reset to put the internal array pointer at the first element. I then get each following element with the next function. This is the order in which the elements exist in memory, but they retain their original indices, which are the numbers starting with zero that were used when the elements were added to the array.
If I had used arsort, the order would have been the exact opposite. It is important to keep the elements in the array returned by getdate associated with their indices. It may not be particularly useful but illustrates the use of this function.
Using the asort Function
Using the asort Function
The last sorting function I want to discuss in this section is ksort. This function sorts an array on the values of the indices. I've modified the code to use ksort instead of arsort. Notice that now all the elements are in the order of their indices, or keys.
Using the ksort Function
The ksort function is perhaps most useful in situations where you have an associative array and you don't have complete control over the contents. The script gets an array generated by the getdate function. If you run it with the ksort line commented out, you will see that the order is arbitrary. It's simply the order chosen when the function was coded.
PHP Related Interview Questions
|PHP Interview Questions||MySQL Interview Questions|
|PHP+MySQL Interview Questions||Drupal Interview Questions|
|MYSQL DBA Interview Questions||PHP5 Interview Questions|
|WordPress Interview Questions||Joomla Interview Questions|
|CakePHP Interview Questions||CodeIgniter Interview Questions|
|PHP7 Interview Questions|
An Introduction To Php
Variables, Operators, And Expressions
Classes And Objects
I/o And Disk Access
Time, Date, And Configuration Functions
Parsing And String Evaluation
Sorting Searching And Random Numbers
Integration With Html
Efficiency And Debugging
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.