Re: [PHP] Sort a multi-dimensional array on a certain key followed by another key

2009-03-28 Thread Virgilio Quilario
 Ok so, I have an array

 [0(index)][1st key][2nd key]

 Basically I don't care about the index. As a matter of fact I'd prefer it
 reset to still be in order afterwards.

 However, I need to sort the 1st key and keep correlation w the second key.
 Then sort on the second key.

 I have video volumes and scenes like

 [0][110][1]
 [1][110][3]
 [2][110][2]
 [3][110][4]

 Any help would be much appreciated.


looks like a job for array_multisort() function
http://php.net/array_multisort

virgil
http://www.jampmark.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Sort a multi-dimensional array on a certain key followed by another key

2009-03-27 Thread TS
Ok so, I have an array

[0(index)][1st key][2nd key]

Basically I don't care about the index. As a matter of fact I'd prefer it
reset to still be in order afterwards.

However, I need to sort the 1st key and keep correlation w the second key.
Then sort on the second key.

I have video volumes and scenes like 

[0][110][1]
[1][110][3]
[2][110][2]
[3][110][4]

Any help would be much appreciated.


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Sort a multi-dimensional array on a certain key followed by another key

2009-03-27 Thread Jim Lucas

TS wrote:

Ok so, I have an array

[0(index)][1st key][2nd key]

Basically I don't care about the index. As a matter of fact I'd prefer it
reset to still be in order afterwards.

However, I need to sort the 1st key and keep correlation w the second key.
Then sort on the second key.

I have video volumes and scenes like 


[0][110][1]
[1][110][3]
[2][110][2]
[3][110][4]

Any help would be much appreciated.




plaintext?php

function recursive_ksort($ar) {
if ( is_array($ar) ) {
ksort($ar);
foreach ( $ar AS $k = $v ) {
if ( is_array($v) ) {
recursive_ksort($v);
$ar[$k] = $v;
}
}
} else {
echo 'ERROR: recursive_ksort() expect the first argument to be 
an array()';
}
return false;
}

$d[0][110][1] = '01101';
$d[0][110][2] = '01102';
$d[0][110][3] = '01103';
$d[1][113][3] = '11103';
$d[1][115][1] = '11101';
$d[1][114][3] = '11103';
$d[2][110][2] = '21102';
$d[3][114][2] = '31102';
$d[2][110][1] = '21101';
$d[3][110][3] = '31103';
$d[2][110][4] = '21104';
$d[3][111][4] = '31104';

recursive_ksort($d);

print_r($d);

?

Seems to work for me.  Give it a run and let us know...


--
Jim Lucas

   Some men are born to greatness, some achieve greatness,
   and some have greatness thrust upon them.

Twelfth Night, Act II, Scene V
by William Shakespeare

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php