Jim from Oakland Posted June 30, 2006 Share Posted June 30, 2006 Phreax:I need to sort the array based on the second dimension key named LNameArray is configured like this:$dataArray[1]['UserID'] = 'dr45';$dataArray[1]['FName'] = 'Joe';$dataArray[1]['LName'] = 'Smith';$dataArray[2]['UserID'] = 'qz50';$dataArray[2]['FName'] = 'Huan';$dataArray[2]['LName'] = 'Lee';Thanks so much! Quote Link to comment https://forums.phpfreaks.com/topic/13324-sorting-an-array/ Share on other sites More sharing options...
Barand Posted June 30, 2006 Share Posted June 30, 2006 Use custom sort function with usort()[code]function sortLName ($a, $b) { return strcmp($a['LName'], $b['LName']);}$dataArray[1]['UserID'] = 'dr45';$dataArray[1]['FName'] = 'Joe';$dataArray[1]['LName'] = 'Smith';$dataArray[2]['UserID'] = 'qz50';$dataArray[2]['FName'] = 'Huan';$dataArray[2]['LName'] = 'Lee';usort ($dataArray, 'sortLName');echo '<pre>', print_r($dataArray, true), '</pre>';[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13324-sorting-an-array/#findComment-51369 Share on other sites More sharing options...
Jim from Oakland Posted June 30, 2006 Author Share Posted June 30, 2006 BarandThat is great! Thanks very much.If you are inclined to share a bit more of your knowledge...Can you give just a bit of explanation about what your approach does?Also I generalized it so it will work with any key.function sort_by_key ($key, $a, $b) { return strcmp($a[$key], $b[$key]);}$dataArray[1]['UserID'] = 'dr45';$dataArray[1]['FName'] = 'Joe';$dataArray[1]['LName'] = 'Smith';$dataArray[2]['UserID'] = 'qz50';$dataArray[2]['FName'] = 'Huan';$dataArray[2]['LName'] = 'Lee';$dataArray[3]['UserID'] = 'mm61';$dataArray[3]['FName'] = 'Have';$dataArray[3]['LName'] = 'Hope';$key = 'LName';usort ($dataArray, sort_by_key($key)); Quote Link to comment https://forums.phpfreaks.com/topic/13324-sorting-an-array/#findComment-51385 Share on other sites More sharing options...
Barand Posted June 30, 2006 Share Posted June 30, 2006 Arguments for usort() are the array and the *name* of your sort function.It passes two array elements at a time to this function ($a, $b) and you function should return -1 if a sorts above b0 if they are considered same+1 if a sorts below bAs this is what strcmp() returns, it's a convenient function when all you need is a simple string comparison.If you want to vary the keyfunction mySort ($a, $b) { global gl_$key; return strcmp($a[$gl_key], $b[$gl_key]);}$gl_key = 'FName';usort ($dataArray, 'mySort');If you want a DESC sort, return -strcmp() Quote Link to comment https://forums.phpfreaks.com/topic/13324-sorting-an-array/#findComment-51395 Share on other sites More sharing options...
Jim from Oakland Posted June 30, 2006 Author Share Posted June 30, 2006 BarandOK. In the spirit of being brief I left out the fact that the array is a class property and that the sorting logic is for a method inthe same class. So, how do I tell usort about the function if "mySort" is a method in the same class as the array? And, presumably the global scoping will work "within" the class?class arrayManager{//loaded by callervar $dataArray;// data_array_sort methodfunction data_array_sort(){ return usort($this->dataArray, 'mySort');}function mySort($a, $b){ global $gSortKey; strcmp($a[$gSortKey], $b[$gSortKey]);}}//end class Quote Link to comment https://forums.phpfreaks.com/topic/13324-sorting-an-array/#findComment-51418 Share on other sites More sharing options...
Barand Posted June 30, 2006 Share Posted June 30, 2006 Try it this way[code=php:0]class arrayManager{ //loaded by caller var $dataArray; var $gSortKey; function arrayManager($data) { $this->dataArray = $data; } // data_array_sort method function data_array_sort($key) { $this->gSortKey = $key; usort($this->dataArray, "arrayManager::mySort"); return $this->dataArray; } static function mySort($a, $b) { return strcmp($a[$this->gSortKey], $b[$this->gSortKey]); } }//end class$dataArray[1]['UserID'] = 'dr45';$dataArray[1]['FName'] = 'Joe';$dataArray[1]['LName'] = 'Smith';$dataArray[2]['UserID'] = 'qz50';$dataArray[2]['FName'] = 'Huan';$dataArray[2]['LName'] = 'Lee';$dataArray[3]['UserID'] = 'mm61';$dataArray[3]['FName'] = 'Have';$dataArray[3]['LName'] = 'Hope';$obj = new arrayManager($dataArray);$sortedData = $obj->data_array_sort('FName');echo '<pre>', print_r($sortedData, true), '</pre>';?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13324-sorting-an-array/#findComment-51443 Share on other sites More sharing options...
Jim from Oakland Posted June 30, 2006 Author Share Posted June 30, 2006 Thanks againI appreciate your assistance, a lot. Unfortunately it is still not working; the array is reordered but not sorted.I note that the static keyword fails on my system; I'm using 4.3.I modified code just a bit to match my class's names.<?class TableData{ var $dataTableArray; var $sortKey; function TableData($data) { $this->dataTableArray = $data; } function data_array_sort($key) { $this->sortKey = $key; usort($this->dataTableArray, "TableData::mySort"); return $this->dataTableArray; } function mySort($a, $b) { return strcmp($a[$this->sortKey], $b[$this->sortKey]); }}//end class$dataArray[1]['UserID'] = 'dr45';$dataArray[1]['FName'] = 'Joe';$dataArray[1]['LName'] = 'Smith';$dataArray[2]['UserID'] = 'qz50';$dataArray[2]['FName'] = 'Huan';$dataArray[2]['LName'] = 'Lee';$dataArray[3]['UserID'] = 'aw13';$dataArray[3]['FName'] = 'Zoey';$dataArray[3]['LName'] = 'Zeus';$dataArray[4]['UserID'] = 'mm61';$dataArray[4]['FName'] = 'Have';$dataArray[4]['LName'] = 'Hope';$obj = new TableData($dataArray);$sortedData = $obj->data_array_sort('LName');echo '<pre>', print_r($sortedData, true), '</pre>';?> Quote Link to comment https://forums.phpfreaks.com/topic/13324-sorting-an-array/#findComment-51463 Share on other sites More sharing options...
Barand Posted July 1, 2006 Share Posted July 1, 2006 The only way I can get it to work with v4 is to specify the key as a literal in the sort function[code=php:0]class TableData{ var $dataTableArray; var $sortKey; function TableData($data) { $this->dataTableArray = $data; } function data_array_sort($key) { $this->sortKey = $key; usort($this->dataTableArray, array('TableData','mySort')); return $this->dataTableArray; } function mySort($a, $b) { return strcmp($a['LName'], $b['LName']); }}//end class$dataArray[1]['UserID'] = 'dr45';$dataArray[1]['FName'] = 'Joe';$dataArray[1]['LName'] = 'Smith';$dataArray[2]['UserID'] = 'qz50';$dataArray[2]['FName'] = 'Huan';$dataArray[2]['LName'] = 'Lee';$dataArray[3]['UserID'] = 'aw13';$dataArray[3]['FName'] = 'Zoey';$dataArray[3]['LName'] = 'Zeus';$dataArray[4]['UserID'] = 'mm61';$dataArray[4]['FName'] = 'Have';$dataArray[4]['LName'] = 'Hope';$obj = new TableData($dataArray);$sortedData = $obj->data_array_sort('LName');echo '<pre>', print_r($sortedData, true), '</pre>';[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13324-sorting-an-array/#findComment-51471 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.