{"id":346,"date":"2013-04-17T12:30:25","date_gmt":"2013-04-17T11:30:25","guid":{"rendered":"http:\/\/www.cotsweb.com\/blog\/?p=346"},"modified":"2013-04-17T12:47:31","modified_gmt":"2013-04-17T11:47:31","slug":"function-to-sort-an-associative-array-in-php","status":"publish","type":"post","link":"https:\/\/www.cotsweb.com\/blog\/function-to-sort-an-associative-array-in-php-346.html","title":{"rendered":"Function to Sort an Associative Array in PHP"},"content":{"rendered":"<p>I needed to sort an associative array into the correct order for display, PHP has various functions for sorting arrays but I couldn&#8217;t find a simple generic one that did what I wanted. Specifically I needed to sort on several keys and wanted a reasonably tidy solution (so much easier to figure out what is going on when you come back it months\/years later).<\/p>\n<h3>The Sort<\/h3>\n<p>Because I was using CodeIgniter for this site I put the following function in helpers\/MY_array_helper.php so I can use it from other parts of the site. But it would work just as well if defined locally.<br \/>\n<pre><code class=\"preserve-code-formatting\">\nfunction assoc_array_sort($a, $b, $key, $sequence = &#039;ASC&#039;)\n{\n\/*\n* Simple function for sorting between 2 values\n* Designed to be called within the function called by usort or uasort\n*\n* $key is the key for the associative array so if we want to compare values of $array[&#039;my_key&#039;]\n* $key would be set to &#039;my_key&#039;\n*\n* Default sequence is ASC but can reverse sort by passing DESC instead\n*\/\nif (strtoupper($sequence) == &#039;DESC&#039;) {\n&nbsp;&nbsp;$lower = 1;\n&nbsp;&nbsp;$higher = -1;\n} else {\n&nbsp;&nbsp;$lower = -1;\n&nbsp;&nbsp;$higher = 1;\n}\n\nif ($a[$key] == $b[$key]) {\n&nbsp;&nbsp;return 0;\n&nbsp;&nbsp;} else if ($a[$key] &amp;lt; $b[$key]) {\n&nbsp;&nbsp;&nbsp;&nbsp;return $lower;\n&nbsp;&nbsp;&nbsp;&nbsp;} else {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return $higher;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n}\n<\/code><\/pre><\/p>\n<h3>Calling the Sort<\/h3>\n<p>The array I needed to sort is called $memberships so the call is;<br \/>\n<pre><code class=\"preserve-code-formatting\">\nuasort($memberships, &#039;membership_sort&#039;);\n<\/code><\/pre><br \/>\nmembership_sort is an intermediate function sitting between uasort (or usort) and assoc_array_sort.  If like me you are using CodeIgniter you will need to load the array_helper before calling this function.<br \/>\n<pre><code class=\"preserve-code-formatting\">\n$this-&gt;load-&gt;helper(&#039;array&#039;);&nbsp;&nbsp; \/\/ load array_helper so we can use the sort in MY_array_helper\n<\/code><\/pre><br \/>\n<pre><code class=\"preserve-code-formatting\">\nfunction membership_sort($x, $y)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/*\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * Use assoc_array_sort to determine sequence\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *&nbsp;&nbsp; but where values match call again for the next variable to be checked.\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * This should give us an array in order\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * - committee_sequence\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * - committee_name\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * - role_sequence\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * - role-name\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *\/\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ret_var = assoc_array_sort($x, $y, &#039;committee_sequence&#039;,&#039;ASC&#039;);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($ret_var == 0) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ret_var = assoc_array_sort($x, $y, &#039;committee_name&#039;,&#039;ASC&#039;);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($ret_var == 0) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ret_var = assoc_array_sort($x, $y, &#039;role_sequence&#039;,&#039;ASC&#039;);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($ret_var == 0) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ret_var = assoc_array_sort($x, $y, &#039;committee_name&#039;,&#039;ASC&#039;);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return $ret_var;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n<\/code><\/pre><br \/>\nYou can see that the first time assoc_array_sort is called it is sorting on $memberships[&#8216;committee_sequence&#8217;], if the values don&#8217;t match it returns -1 or 1 depending on whether the first or second value is higher.  But it the values are equal it drops through to the next sort key $memberships[&#8216;committee_name&#8217;] and repeats the process.<\/p>\n<p>I am sure there are cleverer ways to do this, I would welcome feedback telling me of better ways, but hopefully this will be useful to somebody.<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>I needed to sort an associative array into the correct order for display, PHP has various functions for sorting arrays but I couldn&#8217;t find a simple generic one that did what I wanted. Specifically I needed to sort on several keys and wanted a reasonably tidy solution (so much easier to figure out what is [&hellip;]<!-- 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":[128,19],"tags":[129,151,145,130],"_links":{"self":[{"href":"https:\/\/www.cotsweb.com\/blog\/wp-json\/wp\/v2\/posts\/346"}],"collection":[{"href":"https:\/\/www.cotsweb.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cotsweb.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cotsweb.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cotsweb.com\/blog\/wp-json\/wp\/v2\/comments?post=346"}],"version-history":[{"count":9,"href":"https:\/\/www.cotsweb.com\/blog\/wp-json\/wp\/v2\/posts\/346\/revisions"}],"predecessor-version":[{"id":354,"href":"https:\/\/www.cotsweb.com\/blog\/wp-json\/wp\/v2\/posts\/346\/revisions\/354"}],"wp:attachment":[{"href":"https:\/\/www.cotsweb.com\/blog\/wp-json\/wp\/v2\/media?parent=346"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cotsweb.com\/blog\/wp-json\/wp\/v2\/categories?post=346"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cotsweb.com\/blog\/wp-json\/wp\/v2\/tags?post=346"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}