# Sorting a simple 3d array based on one key value

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
Hi,
I'm looking to sort a 3D array like this:
[
0=3D> [ name=3D>'a' , d=3D>3 ]
2=3D> [ name=3D>'b' , d=3D>1 ]
3=3D> [ name=3D>'c' , d=3D>2 ]
]

by the d key so that it looks like this:
[
2=3D> [ name=3D>'b' , d=3D>1 ]
3=3D> [ name=3D>'c' , d=3D>2 ]
0=3D> [ name=3D>'a' , d=3D>3 ]
]

Any ideas?
Cheers,
Ciar=E1n

Ciaran wrote:

usort

## Re: Sorting a simple 3d array based on one key value

Cheers rf- I had a look at the manual before I posted this but
there are so many array sorting functions, It's difficult to track
down the right one for the job with out having done it before. ANyway,
your post sent em in the right direction so cheers for that.

Here's the solution for anyone interested:

function cmp(\$a, \$b) {
if (\$a =3D \$b) {
return 0;
}
return (\$a['d'] < \$b['d']) ? -1 : 1;
}

\$t=3Darray(
0=3D> array( name=3D>'a' , d=3D>3 ),
2=3D> array( name=3D>'b' , d=3D>1 ),
3=3D> array( name=3D>'c' , d=3D>2 ),
);

print_r(\$t);
uasort(\$t,'cmp');
print_r(\$t);

Cheers,
Ciar=E1n

## Re: Sorting a simple 3d array based on one key value

On Mon, 11 May 2009 19:03:13 -0700 (PDT), Ciaran

Take a look at array_multisort() and search for examples.  Recently I
had a multi-dimensional array to sort (with two keys in opposite
directions) and I used the following WHICH WORKS:

\$PAk=array_keys(\$PA);
\$PAv=array_values(\$PA);
array_multisort(\$PAv, SORT_DESC, SORT_STRING, \$PAk,
SORT_ASC,SORT_STRING);

Play with it and continue to research and you should be able to get
something similar working for you.

Good luck.

## Re: Sorting a simple 3d array based on one key value

i got this from the manual, seems to work good

function sortbycolumn(\$array, \$by, \$order = 'ASC', \$type = 'str'){

//\$array: the array you want to sort
//\$by: the associative array name that is one level deep
////example: d
//\$order: ASC or DESC
//\$type: num or str

\$sortby = "sort\$by"; //This sets up what you are sorting by

\$firstval = current(\$array); //Pulls over the first array

\$vals = array_keys(\$firstval); //Grabs the associate Arrays

foreach (\$vals as \$init){
\$keyname = "sort\$init";
\$\$keyname = array();
}
//Multiple arrays into a variable variable
//I got it to work by initializing the variable variables as arrays
//Before I went any further

foreach (\$array as \$key => \$row) {

foreach (\$vals as \$names){
\$keyname = "sort\$names";
\$test = array();
\$test[\$key] = \$row[\$names];
\$\$keyname = array_merge(\$\$keyname,\$test);

}

}

//This will create dynamic mini arrays so that I can perform
//the array multisort with no problem
//Notice the temp array... I had to do that because I
//cannot assign additional array elements to a
//varaiable variable

if (\$order == "DESC"){
if (\$type == "num"){
array_multisort(\$\$sortby,SORT_DESC, SORT_NUMERIC,\$array);
} else {
array_multisort(\$\$sortby,SORT_DESC, SORT_STRING,\$array);
}
} else {
if (\$type == "num"){
array_multisort(\$\$sortby,SORT_ASC, SORT_NUMERIC,\$array);
} else {
array_multisort(\$\$sortby,SORT_ASC, SORT_STRING,\$array);
}
}