Connecting Tech Pros Worldwide Forums | Help | Site Map

Sorting multidimensional arrays on a named dimension??

markus
Guest
 
Posts: n/a
#1: Jul 17 '05
I usually store info in arrays using field names

$array[1]["country"]='Germany',
$array[1]["code"]="GE";

$array[2]["country"]='United Sates"
$array[2]["code"]="US";

$array[3]["country"]='France"
$array[3]["code"]='FR"

I want to get:
$array[1]["country"]='France"
$array[1]["code"]='FR"

$array[2]["country"]='Germany'
$array[2]["code"]="GE";

$array[3]["country"]='US"
$array[3]["code"]="US";

Is there a function to sort on a field (country in this case)

thanks

Christopher Finke
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Sorting multidimensional arrays on a named dimension??


"markus" <m7484@hotpop.com> wrote in message
news:b226eb71.0407201700.6ba861ff@posting.google.c om...[color=blue]
> I usually store info in arrays using field names
>
> $array[1]["country"]='Germany',
> $array[1]["code"]="GE";
>
> Is there a function to sort on a field (country in this case)[/color]

Read the PHP.net page on the sort() function. There are several
multidimensional sort functions in the user-submitted comments at the
bottom:

http://us3.php.net/manual/en/function.sort.php

Chris Finke


Zurab Davitiani
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Sorting multidimensional arrays on a named dimension??


markus wrote:
[color=blue]
> I usually store info in arrays using field names
>
> $array[1]["country"]='Germany',
> $array[1]["code"]="GE";
>
> $array[2]["country"]='United Sates"
> $array[2]["code"]="US";
>
> $array[3]["country"]='France"
> $array[3]["code"]='FR"
>
> I want to get:
> $array[1]["country"]='France"
> $array[1]["code"]='FR"
>
> $array[2]["country"]='Germany'
> $array[2]["code"]="GE";
>
> $array[3]["country"]='US"
> $array[3]["code"]="US";
>
> Is there a function to sort on a field (country in this case)[/color]

Yes (as in you can use user-defined function for sorting):

http://www.php.net/manual/en/function.usort.php

Example 2.
Closed Thread