nadeemshafi9 Posted April 14, 2009 Share Posted April 14, 2009 hi guys i am trying to sort an array with direction and key, my problem is its not working and i think im using the wriong functions. this is teh structure of teh array $ddiArray2[$tenant][22]["ext"] = "0000"; $ddiArray2[$tenant][22]["ddi"] = "00000"; $ddiArray2[$tenant][22]["rm"] = "dfgfdgfdg"; here is my code if($dir == "ASC") asort($dataArray[$sort]); if($dir == "DESC") arsort($dataArray[$sort]); Thanks for any help Quote Link to comment https://forums.phpfreaks.com/topic/154039-solved-array-sorting-help/ Share on other sites More sharing options...
ober Posted April 14, 2009 Share Posted April 14, 2009 There has to be more to it than that. Your array is $ddiArray2, but you are trying to sort "$dataArray"... ??? Quote Link to comment https://forums.phpfreaks.com/topic/154039-solved-array-sorting-help/#findComment-809737 Share on other sites More sharing options...
nadeemshafi9 Posted April 14, 2009 Author Share Posted April 14, 2009 There has to be more to it than that. Your array is $ddiArray2, but you are trying to sort "$dataArray"... ??? sorry that was my mistake the dataArray is made out of queries in the db based on the ddi array so after all my work i have a working $dataArray i use it in all sorts of components and its basicaly solid. my component in JS calls back to this method with sort paramiters if it was just a SQL statment it owuld be easy, but its an array $ddiArray2 made out of multiple query results. i basicaly need to sort the array on key and direction the structure of both arrays is teh same Quote Link to comment https://forums.phpfreaks.com/topic/154039-solved-array-sorting-help/#findComment-809754 Share on other sites More sharing options...
ober Posted April 14, 2009 Share Posted April 14, 2009 Please show us more of the code so we can understand it better. Quote Link to comment https://forums.phpfreaks.com/topic/154039-solved-array-sorting-help/#findComment-809759 Share on other sites More sharing options...
nadeemshafi9 Posted April 14, 2009 Author Share Posted April 14, 2009 data array is an array of arrays its built in a loop like this $data["id"] = $ddi["ddi"]; $data["count"] = "0"; $data["src"] = $ddi["ddi"]; $data["ext"] = $ddi["ext"]; $data["rm"] = $ddi["rm"]; $dataArray[] = $data; Quote Link to comment https://forums.phpfreaks.com/topic/154039-solved-array-sorting-help/#findComment-809762 Share on other sites More sharing options...
nadeemshafi9 Posted April 14, 2009 Author Share Posted April 14, 2009 foreach($ddiArray2[$tenant] as $k => $ddi){ $where = "(src = '{$ddi["ext"]}' OR src = '{$ddi["ddi"]}') AND calldate >= '{$fromtime}' AND calldate <= '{$totime}' AND billsec > 0 AND LENGTH(dst) > 4"; $select = $cdr_table->select()->from($cdr_table, array('COUNT(src) as count', "id")) ->group('src') ->where($where); $row = $cdr_table->fetchRow($select); if(count($row) >0) { $data = $row->toArray(); $data["src"] = $ddi["ddi"]; $data["ext"] = $ddi["ext"]; $data["rm"] = $ddi["rm"]; $dataArray[] = $data ; } else { $data["id"] = $ddi["ddi"]; $data["count"] = "0"; $data["src"] = $ddi["ddi"]; $data["ext"] = $ddi["ext"]; $data["rm"] = $ddi["rm"]; $dataArray[] = $data; } } Quote Link to comment https://forums.phpfreaks.com/topic/154039-solved-array-sorting-help/#findComment-809766 Share on other sites More sharing options...
nadeemshafi9 Posted April 14, 2009 Author Share Posted April 14, 2009 if($limit == "0"){ $limit = $totalCount; } if($dir == "ASC") sort($dataArray); if($dir == "DESC") rsort($dataArray); $dojoData = new Zend_Dojo_Data('id',array_slice($dataArray, $start, $limit) ,'id'); $dojoData->setMetadata(array('totalCount' => $totalCount)); Quote Link to comment https://forums.phpfreaks.com/topic/154039-solved-array-sorting-help/#findComment-809768 Share on other sites More sharing options...
nadeemshafi9 Posted April 14, 2009 Author Share Posted April 14, 2009 on the front end im using ExtJS datagrid wich is set to remote sorting so it sends the column name and direction when you click teh column Quote Link to comment https://forums.phpfreaks.com/topic/154039-solved-array-sorting-help/#findComment-809771 Share on other sites More sharing options...
nadeemshafi9 Posted April 14, 2009 Author Share Posted April 14, 2009 HERE IS A DUMP Array ( [0] => Array ( [id] => 678678 [count] => 0 [src] => 576765 [ext] => 567657 [rm] => gfjf ) [1] => Array ( [id] => 568769 [count] => 0 [src] => 7487648 [ext] => 5476547 [rm] => fdjfdg ) [2] => Array ( [id] => 657657 [count] => 0 [src] => 457657 [ext] => 6587657 [rm] => rtytrytry ) } Quote Link to comment https://forums.phpfreaks.com/topic/154039-solved-array-sorting-help/#findComment-809778 Share on other sites More sharing options...
nadeemshafi9 Posted April 14, 2009 Author Share Posted April 14, 2009 heres some info http://www.the-art-of-web.com/php/sortarray/ still prety hard Quote Link to comment https://forums.phpfreaks.com/topic/154039-solved-array-sorting-help/#findComment-809788 Share on other sites More sharing options...
nadeemshafi9 Posted April 14, 2009 Author Share Posted April 14, 2009 PHP provides a range of functions that you can use for sorting data based on the key or value of an associative array. Where it gets complicated is when you have an array of associative arrays which you need to sort according to a particular attribute or combination of attributes. This is a very common task for PHP programmers as data returned from database/SQL queries often appears in this format (ref: pg_fetch_assoc and mysql_fetch_assoc). Quote Link to comment https://forums.phpfreaks.com/topic/154039-solved-array-sorting-help/#findComment-809794 Share on other sites More sharing options...
nadeemshafi9 Posted April 14, 2009 Author Share Posted April 14, 2009 im almost there Quote Link to comment https://forums.phpfreaks.com/topic/154039-solved-array-sorting-help/#findComment-809833 Share on other sites More sharing options...
nadeemshafi9 Posted April 15, 2009 Author Share Posted April 15, 2009 DONE: function compare_rm($a, $b) { return strnatcmp($a['rm'], $b['rm']); } function compare_ext($a, $b) { return strnatcmp($a['ext'], $b['ext']); } function compare_src($a, $b) { return strnatcmp($a['src'], $b['src']); } function compare_count($a, $b) { return strnatcmp($a['count'], $b['count']); } if($sort == "rm"){ if($dir == "ASC") usort($dataArray, 'compare_rm'); else if($dir == "DESC") { usort($dataArray, 'compare_rm'); $dataArray = array_reverse($dataArray); } } else if($sort == "ext"){ if($dir == "ASC") usort($dataArray, 'compare_ext'); else if($dir == "DESC") { usort($dataArray, 'compare_ext'); $dataArray = array_reverse($dataArray); } } else if($sort == "src"){ if($dir == "ASC") usort($dataArray, 'compare_src'); else if($dir == "DESC") { usort($dataArray, 'compare_src'); $dataArray = array_reverse($dataArray); } } else if($sort == "count"){ if($dir == "ASC") usort($dataArray, 'compare_count'); else if($dir == "DESC") { usort($dataArray, 'compare_count'); $dataArray = array_reverse($dataArray); } } its a bit wishy washy, php should have a function for this Quote Link to comment https://forums.phpfreaks.com/topic/154039-solved-array-sorting-help/#findComment-810500 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.