Now the krsort() function sorts a given array by keys in descending order. Note that the original keys remain the same unlike in the case of the rsort() function. We make use of the above same array as an example. Now unlike rsort(), the arsort() function keeps the original keys assigned to the respective values they remain unchanged after sorting. ![]() Below we assign our own custom keys (which are eOS versions) to the valuesīut upon sorting, the values get reassigned to the default indices, starting from 0 Similar to sort(), the rsort() function also does not consider the assigned keys they are discarded. The above script prints the below sorted array We take the same array we use for the sort() function and apply rsort() on it. When this function is applied on an array, the elements are reordered from the highest to the lowest. The rsort() function sorts a given array in descending order. In this section, we will explore the functions which does the sorting from the highest to the lowest, i.e., in descending order. To get a properly sorted arrray, we use the natcasesort() function here, which uses a case insensitive natural sorting algorithm. In the resulting sort, the capitalized elements occur first, irrespective of the numbers within them. We capitalize the first letters of the last two elements in the below array and use natsort() to sort them. Now there is one important thing to note when it involves the case of letters: the uppercased letters come first in sorting order. If we use natsort() to sort the above array This is where the natsort() function comes in handy it sorts a given array using a natural order algorithm. You would have expected php2.pdf to come before php11.pdf, but no it doesn't it gets sorted as follows. If you use the sort() function to sort the above array, it will not sort it as you would intend to. The elements are some PDF file names involving numbers. We see that the keys are not ordered this time.Īnd the array gets sorted by keys in ascending order As the array we have used in the above two examples is already sorted by keys, we will juggle it and get it sorted by values. Now there is another function called ksort() which sorts a given array in ascending order by keys. We notice that the original keys are not discarded as in the sort() function they remain associated with their respective values. We make use of the above array as an example. This function sorts the given array in ascending order, all the while maintaining their original index association. Now if we need to retain the original assigned keys, we need to use the asort() function. We have assigned our custom keys, but those keys are discarded and the following array gets printed. To understand this, we modify the above array by assigning customs key values (which are versions) to the values. The sort() function does not consider the keys they are discarded. We recall that all arrays are stored as associative arrays internally in PHP. If your array has both text and numeric values, the numeric values will come last after sorting. NOTE: The sort() function sorts a given array in ascending alphanumeirc order. ![]() The resulting sorted array gets printed as We apply the sort() function on it, and print the resulting array. When this function is applied on an array, the elements are reordered from lowest to highest.Ĭonsider an array consisting of the first four version names of the elementaryOS operating system (based on Ubuntu). The sort() function sorts a given array in ascending order. First, we will look at the functions which sort a given array into ascending order.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |