I've read several post here on sorting multidimensional arrays, but everyone's problem tends to be different, and so are the solutions.
My question is a bit different (I think), and it has to do with efficiency.
I have an array like this:
$MENU['unique_id_1'] = array(
'allow' => 1,
'mod' => "short string: 5-15 chars",
'group' => 11.4,
'label' => "short string: 10-25 chars",
'text' => "long string: 50-100 chars",
'help' => "very long string: 50-1000 chars",
);
$MENU['unique_id_2'] = array(
'allow' => 1,
'mod' => "short string: 5-15 chars",
'group' => 7.2,
'label' => "short string: 10-25 chars",
'text' => "long string: 50-100 chars",
'help' => "very long string: 50-1000 chars",
);
$MENU['unique_id_3'] = array(
'allow' => 1,
'mod' => "short string: 5-15 chars",
'group' => 20.9,
'label' => "short string: 10-25 chars",
'text' => "long string: 50-100 chars",
'help' => "very long string: 50-1000 chars",
);
// and so-on for about 30 unique menu id's...
I need the array ordered by the group key while keeping all associations. I have written my own procedure, but I always thought that it was a bit cumbersome. However, when I looked into other methods using uasort() and array_multisort(), it turned out to take more time than my original method. So I should probably let sleeping dogs lie... but I wanted to make sure I was doing everything correctly. Here is my code for sorting this:
//---------------------------------------------------
// build a $sort array to hold the $MENU group values
//---------------------------------------------------
$sort = array();
foreach ( $MENU as $k => $a )
{
$i = $a['group'];
$sort[ strval( $i ) ] = $k;
}
//-------------------------------------------------------------
// sort the new $sort array by key (which are the group values)
//-------------------------------------------------------------
ksort( $sort );
//------------------------------------------------------------
// build a clone of the $MENU array with the new keys in order
//------------------------------------------------------------
$newMENU = array();
foreach ( $sort as $menu_key )
{
$newMENU[ $menu_key ] = $MENU[ $menu_key ];
}
//---------------------------------------------------
// overwrite the $MENU array with our new ordered one
//---------------------------------------------------
$MENU = $newMENU;
This takes 0.000786 seconds on average. When I tried these other methods, it took about twice as long to execute:
// version 1... about 0.0018 seconds consistently
uasort( $MENU, 'cmp' );
function cmp( $a, $b )
{
return strcmp( $a['group'], $b['group'] );
}
// version 2... about 0.00147 seconds consistently
uasort( $MENU, 'cmp' );
function cmp( $a, $b )
{
if ( $a['group'] == $b['group'] )
{
return 0;
}
return ( $a['group'] < $b['group'] ) ? -1 : 1;
}
// version 3... about 0.00135 seconds consistently
uasort( $MENU, 'cmp' );
function cmp( $a, $b )
{
if ( $a['group'] < $b['group'] )
{
return -1;
}
elseif ( $a['group'] > $b['group'] )
{
return 1;
}
return 0;
}
Of course we are splitting hairs on versions 2 and 3, but this code is part of a very long script and I am trying to trim as much fat as possible. I didn't figure out how to use array_multisort() for this particular situation, but maybe someone else can provide some suggestions about speed and maybe some other considerations regarding the way my first (and fastest) solution is written.
Thanks!