Jump to content

Recommended Posts

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"]

Link to comment
https://forums.phpfreaks.com/topic/63471-array-sort/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/63471-array-sort/#findComment-316311
Share on other sites

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;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/63471-array-sort/#findComment-316322
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.