Page 1 of 1

Two dimensional array sorting?

Posted: Fri Aug 12, 2005 8:37 am
by AAO
Hello,

Well, I'm totally lost on sorting a 2-D array with string values. Here is the array named $filenames.


Array
(
[Ascii] => Array
(
[0] => ABC.htm
[1] => ACC.htm
)

[Hex] => Array
(
[0] => CAA.htm
[1] => AKA.htm
)

[Acs] => Array
(
[0] => CAA.htm
[1] => AKA.htm
)

I want to sort the array, first, outer array - so keys must be arranged like [Acs], [Ascii], and [Hex].

Then, I want to sort the inner array of each outer array without changing its association with the key.

How can I do this? I've tried several functions listed in php.net, but nothing has worked so far.

I really appreciate your help.

Regards,

Posted: Fri Aug 12, 2005 8:41 am
by feyd
multisort(), ksort(), uksort()

Posted: Fri Aug 12, 2005 9:19 am
by neophyte
I tried this one before and wasn't successful. This works for me:

Code: Select all

<?php
$array = array(  'Ascii' => array( 'ABC.htm', 'ACC.htm', 'dog.htm','cat.htm', 'pig.htm'), 
				 'Hex' => array('CAA.htm', 'AKA.htm', 'foo.htm', 'bar.htm'), 
				 'Acs' => array ('CAA.htm', 'AKA.htm', 'car.htm', 'van.htm')); 

ksort($array);
reset($array);
//$r_array = array_map('sort', $array);
while (list( $k, $v) = each($array )){
	if (is_array($v)){
	   array_multisort ($v, SORT_STRING);
	   reset($v);
	}
}
print_r($array);
?>