note 112354 added to function.ksort

From: Date: Thu, 06 Jun 2013 05:46:31 +0000
Subject: note 112354 added to function.ksort
Groups: php.notes 
Request: Send a blank email to php-notes+get-195092@lists.php.net to get a copy of this message
I wrote this function to sort meta_value in wordpress. I tried a lot of array sorting but neither of them work. But this is not suitable for multidimensional array. This is intended only for wordpress meta_value The problem is to sort below( the order should be ascending; alphabetically then numerically like A-Z then 0-9): 500-999 users 25-49 users All Sizes 1-4 users 5-9 users 10-24 users 250-499 users 1000-4999 5000-9999 The solution: function array_sort($arr){ if(is_array($arr)){ $numeric = array(); $string = array(); foreach($arr as $k => $v) { if(isset($v["meta_value"])){ $str = explode(" ",trim($v["meta_value"])); $firstWord = explode("-",trim($str[0])); }else{ $str = $v; $firstWord = explode("-",trim($str)); } $firstWord = $firstWord[0]; if(is_numeric($firstWord)) { $numeric[(int)$firstWord] = $v; }else{ $string[$firstWord] = $v; } unset($firstWord); } ksort($string,SORT_STRING); ksort($numeric,SORT_NUMERIC); return array_merge((array)$string, (array)$numeric); } return false; } The usage: $meta =get_post_meta($post_id,$meta_key); $sorted = array_sort($meta); The result: All Sizes 1-4 users 5-9 users 10-24 users 25-49 users 250-499 users 500-999 users 1000-4999 5000-9999 ---- Server IP: 69.147.83.199 Probable Submitter: 203.215.117.16 ---- Manual Page -- http://php.net/manual/en/function.ksort.php Edit -- https://master.php.net/note/edit/112354 Del: integrated -- https://master.php.net/note/delete/112354/integrated Del: useless -- https://master.php.net/note/delete/112354/useless Del: bad code -- https://master.php.net/note/delete/112354/bad+code Del: spam -- https://master.php.net/note/delete/112354/spam Del: non-english -- https://master.php.net/note/delete/112354/non-english Del: in docs -- https://master.php.net/note/delete/112354/in+docs Del: other reasons-- https://master.php.net/note/delete/112354 Reject -- https://master.php.net/note/reject/112354 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#195092) next »