Course Title: Web Development Masterclass
Course Link:
http://youaccel.com/admin/cdisplay.php?cid=11
Facebook:
https://www.facebook.com/youaccel
Twitter:
https://twitter.com/YouAccel
Website:
http://youaccel.com
Tutorial Content:
PHP offers different methods of sorting the elements in an array.
We will start with the sort() function.
In this example we have an array called names, with
To sort each value in the array alphabetically we can use the sort() function.
To do so, we type sort() and the name of the array in parenthesis.
Next we can use a for loop to output each value in the array.
You can see in the output, the values are all sorted alphabetically.
Note that the sort() function works for numerical values as well.
We can use the rsort() function to reverse-sort the values.
Simply replace sort() with rsort(), save and refresh.
We can sort the elements in an associative array in ascending order, by their value using the asort() function.
Here we have an associative array named tscore. Inside this array we have three elements, each with an assigned value. John- 60, Bill- 80 and Dan - 75.
To sort them in ascending order type asort() with the array name in parenthesis.
Use a foreach loop to output each element in the array.
We can sort by key rather than value, by using the ksort() function. Just replace asort() with ksort().
To sort according to value in descending order use arsort().
To sort according to key in descending order use krsort().
Тэги:
#PHP #PHP_TUTORIALS #LEARN_PHP