Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help! Sorting a Multi Dimensional Associative Array 1

Status
Not open for further replies.

WMAdam

Programmer
Jun 5, 2002
14
0
0
US
Hi Everybody,
I have an array of arrays of size n in the following format:

people_array[0]["name"]["age"]["dob"]
people_array[1]["name"]["age"]["dob"]
people_array[2]["name"]["age"]["dob"]
....
people_array[n-1]["name"]["age"]["dob"]

So, if there are n people, there are n rows in the main array, each row being an array itself of 3 elements (name,age,dob).

Now, I need to know how to sort this array, as it is currently set up, by "name" or "age" or "dob".
The examples I've seen of array_multisort seem to only work when the array is set up much differently than mine.

Any help would be greatly appreciated!
-Adam


 
<paul>
are you retrieving your array input from a database?
i would think if you had a table called 'ppl' and fields named 'name','age','addy' etc you could just 'SELECT age FROM ppl' with a call to the dB
essentially that would be the same as

$ppl = array (
array ( &quot;name&quot; => &quot;Oswald&quot;,
&quot;age&quot; => &quot;51&quot;,
&quot;addy&quot; => &quot;main st&quot;,
array ( &quot;name&quot; => &quot;Bornhald&quot;,
&quot;age&quot; => &quot;12&quot;,
&quot;addy&quot; => &quot;baker st&quot;,
array ( &quot;name&quot; => &quot;Gertrude&quot;,
&quot;age&quot; => &quot;94&quot;,
&quot;addy&quot; => &quot;90210&quot;,
array ( &quot;name&quot; => &quot;Opie&quot;,
&quot;age&quot; => &quot;101&quot;,
&quot;addy&quot; => &quot;Granville&quot;,
);

help at all???
hope so but probably not
</paul>
 
You have to create a function that compares 2 array elements.

then use the function usort.

check the manual in But if you want my advise, change the array to the one suggested by paul. It's easier to work and easier to understand.

But, if you are getting the info from the database, don't trouble in the arrays, just add an order by clause in the SQL and the data will be sorted by the way you want.

If you need more help, just post some relevant code.

Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top