Built-In Sorting Functions PHP

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

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

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

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.



Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

PHP Topics