Help - Search - Members - Calendar
Full Version: PHP: Sorting multdimensional arrays
Invision Power Services > Community Forums > Community Web Design and Coding
Starnox
I am want to sort an array within another array, however the key could be anything. Can you get PHP to sort an array one dimension in whatever it's key is.

E.g.
CODE
$array = array("moo" => array("1"=>"test", "3"=>"doo", "2"=>"as"), "blas"=> array("1"=>"tdfest", "3"=>"deroo", "2"=>"asfg");


if I knew the keys I could do:

krsort($array["moo"]);
krsort($array["blas"]);

But I don't know if the key is moo, blas, or whatever. How can I get it to do sort all the arrays one dimension in?
Stephen
http://uk2.php.net/array_multisort

original.gif
Starnox
QUOTE(Stephen @ Mar 15 2006, 11:25 AM) *


Yes I saw that but you need to know the keys of the internal arrays. Which I don't. There could be two there could be ten.
princetontiger
You can do this:

array_multisort($ar[0], SORT_ASC, SORT_STRING,
$ar[1], SORT_NUMERIC, SORT_DESC);

As shown in the second example... You just have to know the indice.
Starnox
QUOTE(2be1ask1 @ Mar 15 2006, 12:57 PM) *
You can do this:

array_multisort($ar[0], SORT_ASC, SORT_STRING,
$ar[1], SORT_NUMERIC, SORT_DESC);

As shown in the second example... You just have to know the indice.


Which I don't

nvm. I used a foreach to get the key values that I wanted to sort and then I sorted each key via ksort().
Brendon Koz
What do you mean exactly (so I understand explicitly) when you say "one dimension in"?

CODE
foreach($array as $inner_array){
    $array[$inner_array] = krsort($inner_array);
}


Like that?
Starnox
CODE
$array = array("moo" => array("1"=>"test", "3"=>"doo", "2"=>"as"), "blas"=> array("1"=>"tdfest", "3"=>"deroo", "2"=>"asfg");

foreach($array as $id => $inner)
{
    ksort($array[$id]);
}


Like that.
Brendon Koz
Different variable name(s), same solution. biggrin.gif If you don't use the values of the inner array, there's no need to split it in the foreach statement.

If you're never going to be certain of the keys, or the type in the key, you might want to use array_flip, then natsort, then array_flip again. It's a performance hit but it might have a slightly better result set if you need it.

(i.e.: 2 comes before 10 in natsort, 10 comes before 2 in standard sort)

...or you can create your own sort function based on natsort using uksort()...for that matter you could call natsort within uksort I believe.
Starnox
Well I was using a foreach anyway, so I thought it I may as well sort it during the foreach.
Luke
You dont need to use foreach to get the keys, just use the array_keys function.
Starnox
QUOTE(Cy @ Mar 15 2006, 05:32 PM) *
You dont need to use foreach to get the keys, just use the array_keys function.


cheers, however I am using the foreach for something else as well, it's just killing two birds with one stone wink.gif
Brendon Koz
QUOTE(Cy @ Mar 15 2006, 10:32 AM) *
You dont need to use foreach to get the keys, just use the array_keys function.


It's not necessarily the keys themselves that he needs in and of itself. He needed to know the keys (dynamically) in order to sort the array. Using array_keys() would actually obfuscate his code and add an unnecessary layer of abstraction (and inefficiency) for his current purpose.

I highly recommend you all to download the PHP manual in one form or another. On windows platforms, I recommend the CHM. I use it religiously! laughing.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.