What Will I Learn?
I will learn the array methods part 1 we have the sort, rsort fo the indexed arrays and ksort, krsort for the associative arrays , and array reverse, shuffle methods .
- Sort and Rsort methods for the indexed arrays
- Ksort and Krsort for the associative arrays
- Array Reverse and Shuffle methods
- Server support PHP , Xampp or Wamp for example
- An IDE like Sublime text.
- Browser (Chrome for example)
In this tutorial we will continue our series about the PHP programming language, we will talk about the arrays methods "Sort , Rsort " for the indexed arrays , " Ksort , Krsort " for the associative arrays , Reverse and Shuffle methods , let's start ..
1- Sort Method :
This method is used to rank array from the smallest to the largest , it's sensitive to the case of characters, the letter ' A' is smaller than the letter ' Z ' and the letter ' Z ' smaller than ' a ' , if the comparison of two elements is equal their relative order in the ordred array is not specified.
You can pass two parameters for this array , the first is the array that you want to sort and the second is the sorting type and there is many types :
- SORT_REGULAR : To compare items naturally
- SORT_STRING : To compare string items
To use the sort method we need to pass the array as parameter :
The type is by default " SORT_REGULAR" it's optional, it will sort the array in ascending order this is the result of this method .
2- Rsort Method :
The reverse sort method arranges the array in reverse order, maintains the same proprieties as the sort method, from the largest to the smallest, if the comparison of two elements is equal their relative order in the ordred array is not specified.
It has the same sorting types like the previous method, it specifies new keys for elements in the array , it will remove any existing keys you have already specified instead of simply rearranging the keys.
To use the rsort method we need to pass the array as parameter :
I will sort the results in the reverse order
3- Ksort Method :
This method used to rank an associative array in the ascending order, it means from the smallest to the largest, it arranges an array by key, and maintains the association of the indexes with their associated values, which is primarily useful in associative arrays.
It returns ' TRUE ' when it succeeds at work and ' FALSE ' when it fails.
To use the ksort method we need to pass the array as parameter :
I will sort the materials array by key using the ksort method and this is the result
4- Krsort Method :
This method used to rank an associative method in the descending order, it means from the largest to the smallest, it has two parameters the array and the sorting type, we have many other types :
To use the krsort method we need to pass the array as parameter :
To print the array am using the foreach loop and this the result after the reverse sort
5- Array Reverse Method :
The array reverse used to reverse the order of the array, it doesn't do any type of the sorting types, it just reverse the order of keys and values from the top to bottom without any defect in the content.
To use the reverse array we need to define new array and this is the syntax :
$reversedArray = array_reverse($array);
I will reverse the indexed array and the associative array to see the difference between the sort and the reverse
6- Shuffle Method :
The shuffle method mixes the elements of the array by arranging them randomly, a random number genrator is used, when each refresh of the page the array will be arranged differently.
To use the shuffle method we just pass the array as parameter
The shuffle method helps us a lot, to see the benifit of it we will use an example of question game, when you reload the page you will get a different question
$questions = array(
"What's your name ?",
"What's your job ?",
"How old are you ?",
"Where are you from ?"
This is my array we have 4 questions and to display the question I will use this code
echo "<h4>" . $questions . "</h4>";
I will always print the second item in h4 element , after that I have an input with button to answer the question
This is the question in the second position , and when I reload my page this is the result