AP81 Posted August 5, 2007 Share Posted August 5, 2007 Hi guys, Any ideas how I can sort this array by "POST"? Thanks. $clientArray[$i]["CLIENT_CODE"] $clientArray[$i]["CLIENT_NAME"] $clientArray[$i]["SERVICE"] $clientArray[$i]["HARDWARE"] $clientArray[$i]["MUSIC"] $clientArray[$i]["PURCHASE"] $clientArray[$i]["INVOICE_TOT"] $clientArray[$i]["POST"] $clientArray[$i]["RECALL"] $clientArray[$i]["FIRST_INV"] $clientArray[$i]["GROUP_TYPE"] $clientArray[$i]["END_DATE_S"] $clientArray[$i]["MEMO"] Quote Link to comment https://forums.phpfreaks.com/topic/63471-array-sort/ Share on other sites More sharing options...
teng84 Posted August 5, 2007 Share Posted August 5, 2007 what>??????????? Quote Link to comment https://forums.phpfreaks.com/topic/63471-array-sort/#findComment-316297 Share on other sites More sharing options...
AP81 Posted August 5, 2007 Author Share Posted August 5, 2007 I want to sort this array by the key "POST", i.e. If the array below has 100 elements, then I want it ordered by and the array re-indexed. So the array would be ordered like this: $clientArray[0] = 1000 $clientArray[1] = 2000 $clientArray[2] = 3000 $clientArray[3] = 4000 $clientArray[4] = 5000 $clientArray[0][CLIENT_CODE] => XXXXX $clientArray[0][CLIENT_NAME] => XXXXX $clientArray[0][sERVICE] => 0 $clientArray[0][HARDWARE] => 0 $clientArray[0][MUSIC] => 0 $clientArray[0][PURCHASE] => 0 $clientArray[0][iNVOICE_TOT] => 750 $clientArray[0][post] => 4161 $clientArray[0][RECALL] => MONTH $clientArray[0][FIRST_INV] => 01 SEP 92 $clientArray[0][GROUP_TYPE] => MCGU $clientArray[0][END_DATE_S] => $clientArray[0]MEMO] => $clientArray[1][CLIENT_CODE] => XXXXX $clientArray[1][CLIENT_NAME] => XXXXX $clientArray[1][sERVICE] => 0 $clientArray[1][HARDWARE] => 0 $clientArray[1][MUSIC] => 0 $clientArray[1][PURCHASE] => 0 $clientArray[1][iNVOICE_TOT] => 1500 $clientArray[1][post] => 4561 $clientArray[1][RECALL] => MONTH $clientArray[1][FIRST_INV] => 01 SEP 92 $clientArray[1][GROUP_TYPE] => ERH $clientArray[1][END_DATE_S] => UPGRADE $clientArray[1]MEMO] => NEW ONLY Quote Link to comment https://forums.phpfreaks.com/topic/63471-array-sort/#findComment-316311 Share on other sites More sharing options...
teng84 Posted August 5, 2007 Share Posted August 5, 2007 http://www.php.net/manual/en/function.rsort.php http://www.php.net/manual/en/function.sort.php http://www.php.net/manual/en/function.krsort.php http://www.php.net/manual/en/function.ksort.php all that you need is there any more concern? Quote Link to comment https://forums.phpfreaks.com/topic/63471-array-sort/#findComment-316316 Share on other sites More sharing options...
AP81 Posted August 5, 2007 Author Share Posted August 5, 2007 I understand that, but the manual only shows examples with basic associative arrays. Thanks anyway. Quote Link to comment https://forums.phpfreaks.com/topic/63471-array-sort/#findComment-316319 Share on other sites More sharing options...
teng84 Posted August 5, 2007 Share Posted August 5, 2007 example in that pages is the same you jus have to refer the index for the other index inside it in the sample maybe they on use this sort(array here) but you can do this sort($array[0]) or sort($array[0][xxx]) if your dealing with multi dimensional array Quote Link to comment https://forums.phpfreaks.com/topic/63471-array-sort/#findComment-316320 Share on other sites More sharing options...
Barand Posted August 6, 2007 Share Posted August 6, 2007 Not quite all. Missing usort() and other user sort routines, which are the ones needed here. <?php $clientArray[0]['CLIENT_CODE'] = 'XXXXX'; $clientArray[0]['CLIENT_NAME'] = 'XXXXX'; $clientArray[0]['SERVICE'] = 0 ; $clientArray[0]['HARDWARE'] = 0; $clientArray[0]['MUSIC'] = 0; $clientArray[0]['PURCHASE'] = 0 ; $clientArray[0]['INVOICE_TOT'] = 750 ; $clientArray[0]['POST'] = 4961 ; $clientArray[0]['RECALL'] = 'MONTH' ; $clientArray[0]['FIRST_INV'] = '01 SEP 92' ; $clientArray[0]['GROUP_TYPE'] = 'MCGU' ; $clientArray[0]['END_DATE_S'] = ''; $clientArray[0]['MEMO'] = ''; $clientArray[1]['CLIENT_CODE'] = 'XXXXX' ; $clientArray[1]['CLIENT_NAME'] = 'XXXXX' ; $clientArray[1]['SERVICE'] = 0 ; $clientArray[1]['HARDWARE'] = 0 ; $clientArray[1]['MUSIC'] = 0 ; $clientArray[1]['PURCHASE'] = 0 ; $clientArray[1]['INVOICE_TOT'] = 1500 ; $clientArray[1]['POST'] = 4561 ; $clientArray[1]['RECALL'] = 'MONTH' ; $clientArray[1]['FIRST_INV'] = '01 SEP 92' ; $clientArray[1]['GROUP_TYPE'] = 'ERH' ; $clientArray[1]['END_DATE_S'] = 'UPGRADE'; $clientArray[1]['MEMO'] = 'NEW ONLY'; usort ($clientArray, 'postSort'); // sort using custom postSort function /** * check results */ echo '<pre>', print_r($clientArray, true), '</pre>'; function postSort ($a, $b) { if ($a['POST'] == $b['POST']) return 0; return $a['POST'] < $b['POST'] ? -1 : 1; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/63471-array-sort/#findComment-316322 Share on other sites More sharing options...
AP81 Posted August 6, 2007 Author Share Posted August 6, 2007 Thanks Barand, spot on. Quote Link to comment https://forums.phpfreaks.com/topic/63471-array-sort/#findComment-316335 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.