Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-06-2005, 04:40 PM   PM User | #1
dumpfi
Regular Coder

 
Join Date: Jun 2004
Posts: 565
Thanks: 0
Thanked 18 Times in 18 Posts
dumpfi will become famous soon enough
Sorting an multidimensional array

This function sorts a multidimensional (tabular) array by one or more columns.
PHP Code:
<?php
/* usage
    sortDataSet(data set, column1[, mixed arg [, mixed ... [, array ...]]])
*/
/* arguments
    the first argument is the multidimensional array
    subsequent arguments follow the argument order of array_multisort(),
    except that you do not pass arrays to the function but keys (string!) of the columns
*/
/* note
    read the documentation of array_multisort() for more information
*/
function sortDataSet(&$dataSet) {
    
$args func_get_args();
    
$callString 'array_multisort(';
    
$usedColumns = array();
    for(
$i 1$count count($args); $i $count; ++$i) {
        switch(
gettype($args[$i])) {
            case 
'string':
                
$callString .= '$dataSet[\''.$args[$i].'\'], ';
                
array_push($usedColumns$args[$i]);
                break;
            case 
'integer':
                
$callString .= $args[$i].', ';
                break;
            default:
                throw new 
Exception('expected string or integer, given '.gettype($args[$i]));
        }
    }
    foreach(
$dataSet as $column => $array) {
        if(
in_array($column$usedColumns)) continue;
        
$callString .= '$dataSet[\''.$column.'\'], ';
    }
    eval(
substr($callString0, -2).');');
}

/* example usage */
$latestPost = array(
    
'title' => array(
        
'title0',
        
'title1',
        
'title2',
        
'title5',
        
'title3'
    
),
    
'name' => array(
        
'name0',
        
'name1',
        
'name2b',
        
'name2a',
        
'name3'
    
),
    
'date' => array(
        
'0',
        
'1',
        
'2b',
        
'2a',
        
'3'
    
)
);
sortDataSet($latestPost'name'SORT_DESCSORT_STRING'title'SORT_DESCSORT_STRING);
echo 
'<pre>';
print_r($latestPost);
echo 
'</pre>';
?>
dumpfi
dumpfi is offline   Reply With Quote
Old 12-12-2005, 06:35 PM   PM User | #2
redoc
New Coder

 
Join Date: Jan 2005
Location: Iowa City
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
redoc is an unknown quantity at this point
The following shoud do it. pass the array, index is what element you want to sort on (in your case use 3). Then you can sort asc or desc, and weather you want to use natural sorting (the way a human would sort, 2 before 10) and lastly case sensitive or not if using natural sort. Only the first 2 args are needed, the rest will default.

PHP Code:
    function sort2d ($array$index$order='asc'$natsort=FALSE$case_sensitive=FALSE
    {
        if(
is_array($array) && count($array)>0
        {
           foreach(
array_keys($array) as $key
               
$temp[$key]=$array[$key][$index];
               if(!
$natsort
                   (
$order=='asc')? asort($temp) : arsort($temp);
              else 
              {
                 (
$case_sensitive)? natsort($temp) : natcasesort($temp);
                 if(
$order!='asc'
                     
$temp=array_reverse($temp,TRUE);
           }
           foreach(
array_keys($temp) as $key
               (
is_numeric($key))? $sorted[]=$array[$key] : $sorted[$key]=$array[$key];
           return 
$sorted;
      }
      return 
$array;
    } 
__________________
PS - this is what part of the alphabet would look like if Q and R were eliminated.
redoc is offline   Reply With Quote
Old 08-04-2011, 01:14 PM   PM User | #3
odry147
New to the CF scene

 
Join Date: Aug 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
odry147 is an unknown quantity at this point
Wow! Thank you for sharing so usefull information
odry147 is offline   Reply With Quote
Old 11-23-2011, 04:21 PM   PM User | #4
emptypixels
New to the CF scene

 
Join Date: Nov 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
emptypixels is an unknown quantity at this point
One more thing...

When sorting alphabetically, is there a way to ignore the word "The".
I'm running into this issue with a list of company names.

Brookes
Corporation B
Test Industries
The Best Way
Tims Company

Ideally, 'The Best Way' would be sorted by 'Best' and not 'The'.
emptypixels is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:28 PM.


Advertisement
Log in to turn off these ads.