{"id":50,"date":"2010-09-15T15:23:45","date_gmt":"2010-09-15T15:23:45","guid":{"rendered":"http:\/\/www.evermoredevnotes.com\/?p=50"},"modified":"2010-09-15T15:23:45","modified_gmt":"2010-09-15T15:23:45","slug":"php-usort","status":"publish","type":"post","link":"https:\/\/www.evermoretechnologies.com\/blog\/2010\/09\/php-usort\/","title":{"rendered":"php usort"},"content":{"rendered":"<p>Array sorting is always fun.\u00a0 php provides more functions then you can imagine to do array sorting.\u00a0 I recently had to use the<a href=\"http:\/\/php.net\/manual\/en\/function.usort.php\" target=\"_blank\"> usort function<\/a>.\u00a0 It&#8217;s a pretty handy one for sorting single values on their keys so you can end up with apple in index 0 and zebra in index 21, or whatever the last index is.\u00a0 The big key to it is it allows you to write the sort function yourself.<\/p>\n<p>My task was to sort an array of alphanumberic strings by name and number.\u00a0 For example, RCM23 goes before RCM121 and after RCR23.\u00a0 Also, RCM23A needs to go after RCM23 but before RCM 33.\u00a0 In comes usort, with a custom function to sort my array.<\/p>\n<p>[sourcecode language=&#8221;php&#8221;]<br \/>\nusort($files, &quot;arraySortCallback&quot;);  \/\/You can name the function whatever you want.  THe function returns either -1, 0, or 1 to indicate which of the two passed values is larger.<\/p>\n<p>function arraySortCallback($a, $b) {<br \/>\n    $txt1 = substr($a,0,3);<br \/>\n    $txt2 = substr($b,0,3);<br \/>\n    $num1 = (int) preg_replace(&quot;[D]&quot;, &quot;&quot;, $a);<br \/>\n    $num2 = (int) preg_replace(&quot;[D]&quot;, &quot;&quot;, $b);<br \/>\n    if($txt1 === $txt2) {<br \/>\n\t\tif($num1 === $num2) {<br \/>\n\t\t\treturn $a &gt; $b;<br \/>\n\t\t}<br \/>\n\t\telse {<br \/>\n        \treturn $num1 &gt; $num2;<br \/>\n\t\t}<br \/>\n    }<br \/>\n    else {<br \/>\n        return strcmp($txt1, $txt2) &gt; 0;<br \/>\n    }<br \/>\n};<br \/>\n[\/sourcecode]<\/p>\n<p>And with that, the sorts all worked out and products displayed correctly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Array sorting is always fun.\u00a0 php provides more functions then you can imagine to do array sorting.\u00a0 I recently had to use the usort function.\u00a0 It&#8217;s a pretty handy one&#8230; <a href=\"https:\/\/www.evermoretechnologies.com\/blog\/2010\/09\/php-usort\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/50"}],"collection":[{"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/comments?post=50"}],"version-history":[{"count":0,"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/50\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/media?parent=50"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/categories?post=50"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/tags?post=50"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}