Sorting an array of sub-arrays based on a sub-array"s key

Sorting an array of sub-arrays based on a sub-array"s key

am 07.09.2009 00:45:56 von James Colannino

Hey everyone. I have an array that looks like this:

$main_array[0] = array('key1' => 'vala');
$main_array[1] = array('key1' => 'valb');
etc.

I want to sort the main array based on the value of key1 for each
sub-array. I looked at all the array sorting functions, but unless I
misunderstood something, I didn't see a direct way to do what I want.

If there were a sorting function in which I could pass as an argument
the name of a function that compares two elements like qsort in C, I
could do it easily. Is there a function like that in PHP? If not, what
should I do?

Thanks everyone!
James

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

Re: Sorting an array of sub-arrays based on a sub-array"s key

am 07.09.2009 00:47:51 von Eddie Drapkin

On Sun, Sep 6, 2009 at 6:45 PM, James Colannino wrote:
> Hey everyone.  I have an array that looks like this:
>
> $main_array[0] =3D array('key1' =3D> 'vala');
> $main_array[1] =3D array('key1' =3D> 'valb');
> etc.
>
> I want to sort the main array based on the value of key1 for each
> sub-array.  I looked at all the array sorting functions, but unless =
I
> misunderstood something, I didn't see a direct way to do what I want.
>
> If there were a sorting function in which I could pass as an argument
> the name of a function that compares two elements like qsort in C, I
> could do it easily.  Is there a function like that in PHP?  If =
not, what
> should I do?
>
> Thanks everyone!
> James
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

http://us3.php.net/uasort

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

Re: Sorting an array of sub-arrays based on a sub-array"s key

am 07.09.2009 01:03:39 von James Colannino

Eddie Drapkin wrote:

> http://us3.php.net/uasort

Exactly what I was looking for. Thanks.

James

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