By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,306 Members | 1,621 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,306 IT Pros & Developers. It's quick & easy.

sort multi-dimensional array

P: n/a
Hi,

does anyone know how i can sort a multi-dimensional array
by a specific field ?

for example i want to sort arr[m][n] where n=2,
but i need the data of every array to follow that order.

example array:
arr[0][0]
arr[0][1]
arr[0][2]
arr[0][3]
arr[0][4]
arr[0][5]

arr[1][0]
arr[1][1]
arr[1][2]
arr[1][3]
arr[1][4]
arr[1][5]
Aug 25 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Balaskas Evaggelos wrote:
Hi,

does anyone know how i can sort a multi-dimensional array
by a specific field ?

for example i want to sort arr[m][n] where n=2,
but i need the data of every array to follow that order.

example array:
arr[0][0]
arr[0][1]
arr[0][2]
arr[0][3]
arr[0][4]
arr[0][5]

arr[1][0]
arr[1][1]
arr[1][2]
arr[1][3]
arr[1][4]
arr[1][5]
You can use the array_multisort() function to do this. More info and a
number of examples of useage can be found at :
http://uk.php.net/manual/en/function...-multisort.php

Aug 25 '06 #2

P: n/a
gb*******@googlemail.com wrote:
Balaskas Evaggelos wrote:
>Hi,

does anyone know how i can sort a multi-dimensional array
by a specific field ?

for example i want to sort arr[m][n] where n=2,
but i need the data of every array to follow that order.

example array:
arr[0][0]
arr[0][1]
arr[0][2]
arr[0][3]
arr[0][4]
arr[0][5]

arr[1][0]
arr[1][1]
arr[1][2]
arr[1][3]
arr[1][4]
arr[1][5]

You can use the array_multisort() function to do this. More info and a
number of examples of useage can be found at :
http://uk.php.net/manual/en/function...-multisort.php
thanks for your anwser but the first thing i checked,
before i wrote this post, was that url.

The example are for multi-dimensional array of one-dimensional arrays.

I can't find how i can sort a multi-dimensional array of array[m][n]
and for a specific field example n=2.
Aug 25 '06 #3

P: n/a
Balaskas Evaggelos wrote:
thanks for your anwser but the first thing i checked,
before i wrote this post, was that url.

The example are for multi-dimensional array of one-dimensional arrays.

I can't find how i can sort a multi-dimensional array of array[m][n]
and for a specific field example n=2.
Maybe I just am not understanding you correctly, but aren't they the
same thing?
In PHP:
$ar['m']['n'] = 1;
is equivalent to:
$ar = array('m' =array('n' =1));

Both can be referenced as $ar['m']['n'].

Off the top of my head, the most relevant example from that php manual
page would be Example 3. Sorting database results. It sounds like you
want to essentially sort a 'table' of data by one of the columns. The
concepts of that example should point you in the right direction.
Unless, as I said, I am completely misunderstanding your question.

Aug 25 '06 #4

P: n/a
mo*******************@yahoo.com wrote:
Balaskas Evaggelos wrote:
>thanks for your anwser but the first thing i checked,
before i wrote this post, was that url.

The example are for multi-dimensional array of one-dimensional arrays.

I can't find how i can sort a multi-dimensional array of array[m][n]
and for a specific field example n=2.

Maybe I just am not understanding you correctly, but aren't they the
same thing?
In PHP:
$ar['m']['n'] = 1;
is equivalent to:
$ar = array('m' =array('n' =1));

Both can be referenced as $ar['m']['n'].

Off the top of my head, the most relevant example from that php manual
page would be Example 3. Sorting database results. It sounds like you
want to essentially sort a 'table' of data by one of the columns. The
concepts of that example should point you in the right direction.
Unless, as I said, I am completely misunderstanding your question.
maybe i don't write it correctly - bad english :)

in the example 3 the multi-dimensional array breaks into two
one-dimensional arrays to sort them. So the results is
sorted for every separated array.

i need the 2 arrays to be sorted only by the second field of the second
array. And all the rest to be sorted according by this field.

this is my array:

arr[0][0]
arr[0][1]
arr[0][2]
arr[0][3]
arr[0][4]
arr[0][5]

arr[1][0]
arr[1][1]
arr[1][2]
arr[1][3]
arr[1][4]
arr[1][5]

....
....

i want to sort the arr be second element into the arr:

arr[m][2], and everything else will follow this order.

---
as m: 0,1,2,3,4,5,6,7,8,9,...
as n: 0,1,2,3,4,5,6,7,8,9,...
Aug 25 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.