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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux