PHP Tutorial #02 Array Methods (Sort, Rsort, Ksort, Krsort , Array_Reverse And Shuffle)

in #utopian-io6 years ago (edited)

 Image Source 

Repository

 https://github.com/php/php-src  

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

Requirements

  • Server support PHP , Xampp or Wamp for example
  • An IDE like Sublime text.
  • Browser (Chrome for example)

Difficulty

  • Basic

Description 

 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 :

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 .

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 :

rsort($array, type);

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 :

ksort($array, type);

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 :

  • SORT_LOCAL_STRING
  • SORT_NATURAL
  • SORT_FLAG_CASE

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

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 

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 

Video Tutorial

Sort:  

Good work @alexendre-maxim. You were well organized and I feel that your presentation has improved greatly. Good work.

I suggest that you turn off notifications on your computer when you are recording. A couple of times a chime gave me a fright :)

Also, for the "Proof of Work" you presented the video again. I think this may have been an error.

Good work

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thank you @buckydurddle , I am happy to your encourage always , I have copied the wrong link for the proof of work and I have edited it , I will block all the notifications , thank you again sir.

Thanks. Keep up the good work :)

Thank you for your review, @buckydurddle!

So far this week you've reviewed 4 contributions. Keep up the good work!

Hello! Your post has been resteemed and upvoted by @ilovecoding because we love coding! Keep up good work! Consider upvoting this comment to support the @ilovecoding and increase your future rewards! ^_^ Steem On!

Reply !stop to disable the comment. Thanks!

Hey, @alexendre-maxim!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Coin Marketplace

STEEM 0.36
TRX 0.12
JST 0.040
BTC 70846.59
ETH 3567.69
USDT 1.00
SBD 4.79