https://github.com/php/php-src
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 .
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 ..
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 :
To use the sort method we need to pass the array as parameter :
sort($array, type);
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 .
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 :
rsort($array, type);
I will sort the results in the reverse order
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 :
ksort($array, type);
I will sort the materials array by key using the ksort method and this is the result
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 :
krsort($array, type);
To print the array am using the foreach loop and this the result after the reverse sort
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
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
shuffle($array);
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
<?php
shuffle($questions);
echo "<h4>" . $questions[1] . "</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
https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/methods1.php