php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #22006 Multidimensional Array sorting
Submitted: 2003-02-01 23:24 UTC Modified: 2003-02-02 01:06 UTC
Votes:2
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:0 (0.0%)
From: php at richardneill dot org Assigned:
Status: Wont fix Package: Feature/Change Request
PHP Version: 4.2.0 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: php at richardneill dot org
New email:
PHP Version: OS:

 

 [2003-02-01 23:24 UTC] php at richardneill dot org
Consider an array of arrays : myarray["$i"]["$j"]...["$n"]
I'd like to see a function which will allow me to sort this on the nth array index. Something like:

my_array_function(myarray, n) where n is the dimension to sort on. 

This would make it easy to sort any array by row, column, depth, etc

Sorry if this feature already exists - I have spend 2 hours reading the manual and googling for it to no avail. I know there are ways to get around it, but a dedicated function would be nice. Thanks.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-02-01 23:48 UTC] php at richardneill dot org
Oops - that doesn't quite make sense. We want to be able to sort on "the 3rd column" or "the 7th row" if it's 2-D; in the case of 3-D, we need to be able to sort on "(the line determined by depth=4 and height=3)". So a better prototype would be:

my_array_sort(my_array,i,j,-,l)

meaning: sort by index k, where the other indices have values of i,j,l respectively)




[Array multisort will work, but only to sort by column; therefore if one wants to sort by ROW, the array must be redesigned].

Sorry the first comment isn't mathematically sensible!
 [2003-02-02 01:06 UTC] pollita@php.net
There are many examples and user comments on the manual page for usort() which show how to accomplish this using existing functionality.

http://www.php.net/manual/en/function.usort.php
 [2003-02-02 14:26 UTC] php at richardneill dot org
That's quite true - you can do it. But it's not straightforward, especially when the array has 3 or more dimensions. How about the following:

1)Write a function to do it the way I suggested.

2)Put this in the documentation as a pseudo array-function.

That has the advantages of:
i)Minimum development work required
ii)The function is there as needed (and can be searched for conveniently)
iii)Really good documentation.

Regards

Richard

p.s. My proposal only works when sorting along a line parallel to a cartesian axis of the array. Of course, one could sort according to any line which cuts the n-space of the array - but, while that is more general, its use would be somewhat rare.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 15:01:34 2025 UTC