{"id":595,"date":"2023-03-31T06:21:44","date_gmt":"2023-03-31T06:21:44","guid":{"rendered":"https:\/\/mouryasolutions.in\/blog\/?p=595"},"modified":"2023-03-31T06:46:54","modified_gmt":"2023-03-31T06:46:54","slug":"sort-multidimensional-arrays-in-php","status":"publish","type":"post","link":"https:\/\/mouryasolutions.in\/blog\/sort-multidimensional-arrays-in-php\/","title":{"rendered":"Sort multidimensional arrays in php"},"content":{"rendered":"<div class=\"post-content\">\n<p>In PHP, you can sort multidimensional arrays using the <code>array_multisort()<\/code> function. Here&#8217;s an example of how to sort a multidimensional array in ascending order based on a specific column:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Sample multidimensional array\n$data = array(\n    array('id' =&gt; 1, 'name' =&gt; 'John', 'age' =&gt; 25),\n    array('id' =&gt; 2, 'name' =&gt; 'Mary', 'age' =&gt; 20),\n    array('id' =&gt; 3, 'name' =&gt; 'Peter', 'age' =&gt; 30)\n);\n\n\/\/ Sort the array in ascending order based on age\n$age = array_column($data, 'age');\narray_multisort($age, SORT_ASC, $data);\n\n\/\/ Print the sorted array\nprint_r($data);\n<\/code><\/pre>\n\n\n\n<p>In this example, we first define a multidimensional array <code>$data<\/code> with three columns: <code>id<\/code>, <code>name<\/code>, and <code>age<\/code>. We then use the <code>array_column()<\/code> function to extract the <code>age<\/code> column from the array and store it in a separate array <code>$age<\/code>. Finally, we use the <code>array_multisort()<\/code> function to sort the original array <code>$data<\/code> based on the values in <code>$age<\/code>.<\/p>\n\n\n\n<p>You can also sort the array in descending order by changing <code>SORT_ASC<\/code> to <code>SORT_DESC<\/code>. Additionally, you can sort the array based on multiple columns by passing multiple arrays to <code>array_multisort()<\/code>.<\/p>\n\n\n\n<p>You can sort a multidimensional array <strong>without using<\/strong> a built-in function like <code><strong>array_multisort()<\/strong><\/code>, Just using a <strong>custom <\/strong>sorting function<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Sample multidimensional array\n$data = array(\n    array('id' =&gt; 1, 'name' =&gt; 'John', 'age' =&gt; 25),\n    array('id' =&gt; 2, 'name' =&gt; 'Mary', 'age' =&gt; 20),\n    array('id' =&gt; 3, 'name' =&gt; 'Peter', 'age' =&gt; 30)\n);\n\n\/\/ Define a custom sorting function\nfunction sortByAge($a, $b) {\n    return $a&#91;'age'] - $b&#91;'age'];\n}\n\n\/\/ Sort the array in ascending order based on age\nusort($data, 'sortByAge');\n\n\/\/ Print the sorted array\nprint_r($data);<\/code><\/pre>\n\n\n\n<p>Note: The original keys of the array will be lost after sorting. If you want to preserve the keys, use the <code>uasort()<\/code> function instead of <code>array_multisort()<\/code>.<\/p>\n<\/div><!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>In PHP, you can sort multidimensional arrays using the array_multisort()<a href=\"https:\/\/mouryasolutions.in\/blog\/sort-multidimensional-arrays-in-php\/\">Read More<i class=\"fa fa-long-arrow-right\" aria-hidden=\"true\"><\/i><\/a><!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[10],"tags":[],"_links":{"self":[{"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/posts\/595"}],"collection":[{"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/comments?post=595"}],"version-history":[{"count":3,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/posts\/595\/revisions"}],"predecessor-version":[{"id":602,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/posts\/595\/revisions\/602"}],"wp:attachment":[{"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/media?parent=595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/categories?post=595"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/tags?post=595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}