{"id":15678,"date":"2020-07-03T18:33:09","date_gmt":"2020-07-03T18:33:09","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=15678"},"modified":"2022-04-28T09:15:23","modified_gmt":"2022-04-28T09:15:23","slug":"how-can-i-sort-arrays-and-data-in-php","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/how-can-i-sort-arrays-and-data-in-php\/","title":{"rendered":"How can I sort arrays and data in PHP?"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">PHP Functions For Sorting Arrays<\/h3>\n\n\n\n<p>In the previous chapter you&#8217;ve learnt the essentials of PHP arrays i.e. what arrays are, how to create them, how to view their structure, how to access their elements etc. You can do even more things with arrays like sorting the elements in any order you like.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/07\/sorting-.png\" alt=\"\" class=\"wp-image-15679\"\/><\/figure>\n\n\n\n<p>PHP comes with a number of built-in functions designed specifically for sorting array elements in different ways like alphabetically or numerically in ascending or descending order. Here we&#8217;ll explore some of these functions most commonly used for sorting arrays.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>sort()<\/code>&nbsp;and&nbsp;<code>rsort()<\/code>&nbsp;\u2014 For sorting indexed arrays<\/li><li><code>asort()<\/code>&nbsp;and&nbsp;<code>arsort()<\/code>&nbsp;\u2014 For sorting associative arrays by value<\/li><li><code>ksort()<\/code>&nbsp;and&nbsp;<code>krsort()<\/code>&nbsp;\u2014 For sorting associative arrays by key<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/07\/sorting-function.png\" alt=\"\" class=\"wp-image-15680\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Sorting Indexed Arrays in Ascending Order<\/h3>\n\n\n\n<p>The&nbsp;<code>sort()<\/code>&nbsp;function is used for sorting the elements of the indexed array in ascending order (alphabetically for letters and numerically for numbers).<\/p>\n\n\n\n<p><strong>Example: <\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/70b27e37ae03f235dc4fdb7e7aaad7fc.js\"><\/script>\n\n\n\n<p><strong>This&nbsp;<code>print_r()<\/code>&nbsp;statement gives the following output:<\/strong><\/p>\n\n\n\n<p>Array ( [0] =&gt; Blue [1] =&gt; Green [2] =&gt; Red [3] =&gt; Yellow )<\/p>\n\n\n\n<p><strong>Similarly you can sort the numeric elements of the array in ascending order.<\/strong><\/p>\n\n\n\n<p><strong>Example<\/strong>:-<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/3b7bd39aa4061a15adc3087fc3ca1824.js\"><\/script>\n\n\n\n<p><strong>This&nbsp;<code>print_r()<\/code>&nbsp;statement gives the following output:<\/strong><\/p>\n\n\n\n<p>Array ( [0] =&gt; 1 [1] =&gt; 2 [2] =&gt; 2.5 [3] =&gt; 4 [4] =&gt; 7 [5] =&gt; 10 )<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Sorting Indexed Arrays in Descending Order<\/h3>\n\n\n\n<p>The&nbsp;<code>rsort()<\/code>&nbsp;function is used for sorting the elements of the indexed array in descending order (alphabetically for letters and numerically for numbers).<\/p>\n\n\n\n<p><strong>Example:-<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/c247c8c7f0e60b3a24c1aa2e71abd88b.js\"><\/script>\n\n\n\n<p>This&nbsp;<code>print_r()<\/code>&nbsp;statement gives the following output:<\/p>\n\n\n\n<p>Array ( [0] =&gt; Yellow [1] =&gt; Red [2] =&gt; Green [3] =&gt; Blue )<\/p>\n\n\n\n<p><strong>Similarly, you can sort the numeric elements of the array in descending order.<\/strong><\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/6c81c57c654239f7998b16c34eb8b963.js\"><\/script>\n\n\n\n<p>This&nbsp;<code>print_r()<\/code>&nbsp;statement gives the following output:<\/p>\n\n\n\n<p>Array ( [0] =&gt; 10 [1] =&gt; 7 [2] =&gt; 4 [3] =&gt; 2.5 [4] =&gt; 2 [5] =&gt; 1 )<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Sorting Associative Arrays in Ascending Order By Value<\/h3>\n\n\n\n<p>The&nbsp;<code>asort()<\/code>&nbsp;function sorts the elements of an associative array in ascending order according to the value. It works just like&nbsp;<code>sort()<\/code>, but it preserves the association between keys and its values while sorting.<\/p>\n\n\n\n<p><strong>Example:-<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/e75823d2d4e5daaded9be09777820042.js\"><\/script>\n\n\n\n<p>This&nbsp;<code>print_r()<\/code>&nbsp;statement gives the following output:<\/p>\n\n\n\n<p>Array ( [Harry] =&gt; 14 [Peter] =&gt; 20 [Clark] =&gt; 35 [John] =&gt; 45 )<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Sorting Associative Arrays in Descending Order By Value<\/h3>\n\n\n\n<p>The&nbsp;<code>arsort()<\/code>&nbsp;function sorts the elements of an associative array in descending order according to the value. It works just like&nbsp;<code>rsort()<\/code>, but it preserves the association between keys and its values while sorting.<\/p>\n\n\n\n<p><strong>Example:- <\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/ae434154d290db98ee469d251175b6ce.js\"><\/script>\n\n\n\n<p>This&nbsp;<code>print_r()<\/code>&nbsp;statement gives the following output:<\/p>\n\n\n\n<p>Array ( [John] =&gt; 45 [Clark] =&gt; 35 [Peter] =&gt; 20 [Harry] =&gt; 14 )<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Sorting Associative Arrays in Ascending Order By Key<\/h3>\n\n\n\n<p>The&nbsp;<code>ksort()<\/code>&nbsp;function sorts the elements of an associative array in ascending order by their keys. It preserves the association between keys and its values while sorting, same as&nbsp;<code>asort()<\/code>&nbsp;function.<\/p>\n\n\n\n<p><strong>Example:-<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/f948a09567c11e7622b9ec35a0ed1a14.js\"><\/script>\n\n\n\n<p>This&nbsp;<code>print_r()<\/code>&nbsp;statement gives the following output:<\/p>\n\n\n\n<p>Array ( [Clark] =&gt; 35 [Harry] =&gt; 14 [John] =&gt; 45 [Peter] =&gt; 20 )<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Sorting Associative Arrays in Descending Order By Key<\/h3>\n\n\n\n<p>The&nbsp;<code>krsort()<\/code>&nbsp;function sorts the elements of an associative array in descending order by their keys. It preserves the association between keys and its values while sorting, same as&nbsp;<code>arsort()<\/code>&nbsp;function.<\/p>\n\n\n\n<p><strong>Example:-<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/f5a03678ac46fc85c904462abfe1dd76.js\"><\/script>\n\n\n\n<p>This&nbsp;<code>print_r()<\/code>&nbsp;statement gives the following output:<\/p>\n\n\n\n<p>Array ( [Peter] => 20 [John] => 45 [Harry] => 14 [Clark] => 35 )<\/p>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_23068\"><iframe loading=\"lazy\"  id=\"_ytid_12011\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_23068\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>PHP Functions For Sorting Arrays In the previous chapter you&#8217;ve learnt the essentials of PHP arrays i.e. what arrays are, how to create them, how to view their structure, how&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[5150],"tags":[6200,6201,6202,6199],"class_list":["post-15678","post","type-post","status-publish","format-standard","hentry","category-php","tag-ascending-order","tag-descending-order","tag-reverse-order","tag-sorting-arrays"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/15678","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=15678"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/15678\/revisions"}],"predecessor-version":[{"id":24693,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/15678\/revisions\/24693"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=15678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=15678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=15678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}