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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Wed May 15 04:01:33 2024 UTC