create account

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

View this thread on: hive.blogpeakd.comecency.com
· @alexendre-maxim · (edited)
$23.18
PHP Tutorial #02 Array Methods (Sort, Rsort, Ksort, Krsort , Array_Reverse And Shuffle)
<html>
<p><img src="https://steemitimages.com/0x0/https://bbvaopen4u.com/sites/default/files/img/new/bbva-open4u-herramientas-basicas-desarrolladores-php_1.jpg" width="2122" height="1415"/></p>
<p>&nbsp;<a href="https://bbvaopen4u.com/sites/default/files/img/new/bbva-open4u-herramientas-basicas-desarrolladores-php_1.jpg">Image Source</a>&nbsp;</p>
<h2>Repository</h2>
<p>&nbsp;<a href="https://github.com/php/php-src">https://github.com/php/php-src</a> &nbsp;</p>
<h2>What Will I Learn?</h2>
<p>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 .</p>
<ul>
  <li>Sort and Rsort methods for the indexed arrays</li>
  <li>Ksort and Krsort for the associative arrays</li>
  <li>Array Reverse and Shuffle methods</li>
</ul>
<h2>Requirements</h2>
<ul>
  <li>Server support PHP , Xampp or Wamp for example</li>
  <li>An IDE like Sublime text.</li>
  <li>Browser (Chrome for example)</li>
</ul>
<h2>Difficulty</h2>
<ul>
  <li>Basic</li>
</ul>
<h2>Description&nbsp;</h2>
<p>&nbsp;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 ..</p>
<h3>1- Sort Method :</h3>
<p>&nbsp;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.</p>
<p>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 :&nbsp;</p>
<ul>
  <li>SORT_REGULAR : To compare items naturally</li>
  <li>SORT_STRING : To compare string items</li>
</ul>
<p>To use the sort method we need to pass the array as parameter :</p>
<p><code>sort($array, type);</code></p>
<p>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 .</p>
<p><img src="https://e.top4top.net/p_980xgaos1.png" width="579" height="329"/></p>
<h3>2- Rsort Method :</h3>
<p>The reverse sort method arranges the array in reverse order, maintains the same proprieties as the sort method, from the largest to the smallest, &nbsp;if the comparison of two elements is equal their relative order in the ordred array is not specified.</p>
<p>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.</p>
<p>To use the rsort method we need to pass the array as parameter :</p>
<p><code>rsort($array, type);</code></p>
<p>I will sort the results in the reverse order</p>
<p><img src="https://e.top4top.net/p_980irdrl1.png" width="869" height="320"/></p>
<h3>3- Ksort Method :</h3>
<p>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.</p>
<p>It returns ' TRUE ' when it succeeds at work and ' FALSE ' when it fails.</p>
<p>To use the ksort method we need to pass the array as parameter :</p>
<p><code>ksort($array, type);</code></p>
<p>I will sort the materials array by key using the ksort method and this is the result&nbsp;</p>
<p><img src="https://e.top4top.net/p_980ndd1g1.png" width="540" height="303"/></p>
<h3>4- Krsort Method :</h3>
<p>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 :</p>
<ul>
  <li>SORT_LOCAL_STRING</li>
  <li>SORT_NATURAL</li>
  <li>SORT_FLAG_CASE</li>
</ul>
<p>To use the krsort method we need to pass the array as parameter :</p>
<p><code>krsort($array, type);</code></p>
<p>To print the array am using the foreach loop and this the result after the reverse sort</p>
<p><img src="https://b.top4top.net/p_980upxvt1.png" width="802" height="296"/></p>
<h3>5- Array Reverse Method :</h3>
<p>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.</p>
<p>To use the reverse array we need to define new array and this is the syntax :</p>
<p><code>$reversedArray = array_reverse($array);</code></p>
<p>I will reverse the indexed array and the associative array to see the difference between the sort and the reverse&nbsp;</p>
<p><img src="https://c.top4top.net/p_980czw371.png" width="1208" height="631"/></p>
<h3>6- Shuffle Method :</h3>
<p>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.</p>
<p>To use the shuffle method we just pass the array as parameter&nbsp;</p>
<p><code>shuffle($array);</code></p>
<p>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</p>
<p><code>$questions = array(</code></p>
<p><code>"What's your name ?",</code></p>
<p><code>"What's your job ?",</code></p>
<p><code>"How old are you ?",</code></p>
<p><code>"Where are you from ?"</code></p>
<p><code>);</code></p>
<p>This is my array we have 4 questions and to display the question I will use this code</p>
<p><code>&lt;?php</code></p>
<p><code>shuffle($questions);</code></p>
<p><code>echo "&lt;h4&gt;" . $questions[1] . "&lt;/h4&gt;";</code></p>
<p><code>?&gt;</code></p>
<p>I will always print the second item in h4 element , after that I have an input with button to answer the question</p>
<p><img src="https://c.top4top.net/p_980pgmfm1.png" width="312" height="147"/></p>
<p>This is the question in the second position , and when I reload my page this is the result&nbsp;</p>
<p><img src="https://d.top4top.net/p_980wol6x2.png" width="299" height="155"/></p>
<h2>Video Tutorial</h2>
<p>https://www.youtube.com/watch?v=TiSwlkzNJto&amp;t=11s</p>
<h2>Curriculum</h2>
<ul>
  <li>&nbsp;<a href="https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-01-indexed-arrays-associative-arrays-and-multidimensional-arrays">PHP Tutorial #01 Indexed Arrays, Associative Arrays and Multidimensional Arrays</a></li>
</ul>
<h2>Proof of Work Done</h2>
<p>https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/methods1.php</p>
</html>
πŸ‘  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authoralexendre-maxim
permlinkphp-tutorial-02-array-methods-sort-rsort-ksort-krsort-arrayreverse-and-shuffle
categoryutopian-io
json_metadata{"tags":["utopian-io","video-tutorials","video","programming"],"image":["https://steemitimages.com/0x0/https://bbvaopen4u.com/sites/default/files/img/new/bbva-open4u-herramientas-basicas-desarrolladores-php_1.jpg","https://e.top4top.net/p_980xgaos1.png","https://e.top4top.net/p_980irdrl1.png","https://e.top4top.net/p_980ndd1g1.png","https://b.top4top.net/p_980upxvt1.png","https://c.top4top.net/p_980czw371.png","https://c.top4top.net/p_980pgmfm1.png","https://d.top4top.net/p_980wol6x2.png","https://img.youtube.com/vi/TiSwlkzNJto/0.jpg"],"links":["https://bbvaopen4u.com/sites/default/files/img/new/bbva-open4u-herramientas-basicas-desarrolladores-php_1.jpg","https://github.com/php/php-src","https://www.youtube.com/watch?v=TiSwlkzNJto&t=11s","https://steemit.com/utopian-io/@alexendre-maxim/php-tutorial-01-indexed-arrays-associative-arrays-and-multidimensional-arrays","https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/methods1.php"],"app":"steemit/0.1","format":"html"}
created2018-09-07 23:57:21
last_update2018-09-11 21:41:06
depth0
children8
last_payout2018-09-14 23:57:21
cashout_time1969-12-31 23:59:59
total_payout_value17.580 HBD
curator_payout_value5.597 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length6,788
author_reputation18,071,828,077,109
root_title"PHP Tutorial #02 Array Methods (Sort, Rsort, Ksort, Krsort , Array_Reverse And Shuffle)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id70,652,269
net_rshares22,359,574,458,836
author_curate_reward""
vote details (61)
@buckydurddle ·
$8.31
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](https://join.utopian.io/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](https://review.utopian.io/result/9/222422323).

---- 
Need help? Write a ticket on https://support.utopian.io/. 
Chat with us on [Discord](https://discord.gg/uTyJkNm). 
[[utopian-moderator]](https://join.utopian.io/)
πŸ‘  , , , , , , ,
properties (23)
authorbuckydurddle
permlinkre-alexendre-maxim-php-tutorial-02-array-methods-sort-rsort-ksort-krsort-arrayreverse-and-shuffle-20180911t203725368z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"users":["alexendre-maxim"],"links":["https://join.utopian.io/guidelines","https://review.utopian.io/result/9/222422323","https://support.utopian.io/","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"app":"steemit/0.1"}
created2018-09-11 20:37:24
last_update2018-09-11 20:37:24
depth1
children3
last_payout2018-09-18 20:37:24
cashout_time1969-12-31 23:59:59
total_payout_value6.271 HBD
curator_payout_value2.036 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length855
author_reputation101,965,608,464,038
root_title"PHP Tutorial #02 Array Methods (Sort, Rsort, Ksort, Krsort , Array_Reverse And Shuffle)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id71,019,195
net_rshares8,106,518,986,324
author_curate_reward""
vote details (8)
@alexendre-maxim ·
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.
πŸ‘  
properties (23)
authoralexendre-maxim
permlinkre-buckydurddle-re-alexendre-maxim-php-tutorial-02-array-methods-sort-rsort-ksort-krsort-arrayreverse-and-shuffle-20180911t214404991z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"users":["buckydurddle"],"app":"steemit/0.1"}
created2018-09-11 21:44:24
last_update2018-09-11 21:44:24
depth2
children1
last_payout2018-09-18 21:44:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length195
author_reputation18,071,828,077,109
root_title"PHP Tutorial #02 Array Methods (Sort, Rsort, Ksort, Krsort , Array_Reverse And Shuffle)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id71,023,527
net_rshares4,668,046,361
author_curate_reward""
vote details (1)
@buckydurddle ·
Thanks. Keep up the good work :)
properties (22)
authorbuckydurddle
permlinkre-alexendre-maxim-re-buckydurddle-re-alexendre-maxim-php-tutorial-02-array-methods-sort-rsort-ksort-krsort-arrayreverse-and-shuffle-20180911t214903673z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-09-11 21:49:03
last_update2018-09-11 21:49:03
depth3
children0
last_payout2018-09-18 21:49:03
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length32
author_reputation101,965,608,464,038
root_title"PHP Tutorial #02 Array Methods (Sort, Rsort, Ksort, Krsort , Array_Reverse And Shuffle)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id71,023,775
net_rshares0
@utopian-io ·
Thank you for your review, @buckydurddle!

So far this week you've reviewed 4 contributions. Keep up the good work!
properties (22)
authorutopian-io
permlinkre-re-alexendre-maxim-php-tutorial-02-array-methods-sort-rsort-ksort-krsort-arrayreverse-and-shuffle-20180911t203725368z-20180916t025212z
categoryutopian-io
json_metadata"{"app": "beem/0.19.42"}"
created2018-09-16 02:52:12
last_update2018-09-16 02:52:12
depth2
children0
last_payout2018-09-23 02:52:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length115
author_reputation152,955,367,999,756
root_title"PHP Tutorial #02 Array Methods (Sort, Rsort, Ksort, Krsort , Array_Reverse And Shuffle)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id71,411,123
net_rshares0
@ilovecoding ·
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! 
 ![](https://codingforspeed.com/images/i-love-coding.jpg) 
*Reply !stop to disable the comment. Thanks!*
πŸ‘  ,
properties (23)
authorilovecoding
permlink20180907t235736394z
categoryutopian-io
json_metadata{"tags":["ilovecoding"],"app":"ilovecoding"}
created2018-09-07 23:57:36
last_update2018-09-07 23:57:36
depth1
children1
last_payout2018-09-14 23:57:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length323
author_reputation40,845,997,808
root_title"PHP Tutorial #02 Array Methods (Sort, Rsort, Ksort, Krsort , Array_Reverse And Shuffle)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id70,652,282
net_rshares7,221,874,979
author_curate_reward""
vote details (2)
@alexendre-maxim ·
Thank you
properties (22)
authoralexendre-maxim
permlinkre-ilovecoding-20180907t235736394z-20180907t235904444z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-09-07 23:59:21
last_update2018-09-07 23:59:21
depth2
children0
last_payout2018-09-14 23:59:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length9
author_reputation18,071,828,077,109
root_title"PHP Tutorial #02 Array Methods (Sort, Rsort, Ksort, Krsort , Array_Reverse And Shuffle)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id70,652,366
net_rshares0
@utopian-io ·
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](https://chrome.google.com/webstore/detail/steemplus/mjbkjgcplmaneajhcbegoffkedeankaj?hl=en) or [Steeditor](https://steeditor.app)).

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

<a href='https://steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1'>Vote for Utopian Witness!</a>
properties (22)
authorutopian-io
permlinkre-php-tutorial-02-array-methods-sort-rsort-ksort-krsort-arrayreverse-and-shuffle-20180911t211043z
categoryutopian-io
json_metadata"{"app": "beem/0.19.42"}"
created2018-09-11 21:10:45
last_update2018-09-11 21:10:45
depth1
children1
last_payout2018-09-18 21:10:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length597
author_reputation152,955,367,999,756
root_title"PHP Tutorial #02 Array Methods (Sort, Rsort, Ksort, Krsort , Array_Reverse And Shuffle)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id71,021,425
net_rshares0
@alexendre-maxim ·
Thank you
properties (22)
authoralexendre-maxim
permlinkre-utopian-io-re-php-tutorial-02-array-methods-sort-rsort-ksort-krsort-arrayreverse-and-shuffle-20180911t211043z-20180911t214437658z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-09-11 21:44:57
last_update2018-09-11 21:44:57
depth2
children0
last_payout2018-09-18 21:44:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length9
author_reputation18,071,828,077,109
root_title"PHP Tutorial #02 Array Methods (Sort, Rsort, Ksort, Krsort , Array_Reverse And Shuffle)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id71,023,552
net_rshares0