开发者

How can I sort columns in a multidimensional array?

开发者 https://www.devze.com 2023-04-10 17:58 出处:网络
I would like to sort a multidimensional array in php like you sort columns in a spreadsheet. I need to be able to select a start and stop column, and optionally, if the \"children\" of a column get so

I would like to sort a multidimensional array in php like you sort columns in a spreadsheet. I need to be able to select a start and stop column, and optionally, if the "children" of a column get sorted. I'm not sure how I should explain this properly so I try with an example.

Can I do this with standard PHP functions, or do I need write my ow开发者_Go百科n? In case of the latter, I would appreciate some help on now to do it.

-------------0------1-------2--- (dimensions)
$someArray[$col1][$col22][$col3];

col1  col2  col3:
a     1     w
c     5     x
b     2     y
d     3     z

unknownSortFunction($someArray, 0, 1, $link=0) // Sort col1 first and then col2 leave col3     (and any "childs") "linked" to column 2.
expected results:
col1  col2  col3
a     1     w
b     2     y
c     3     z
d     5     x

Optionally I would like to be able to sort without keeping col3 "linked" to col2, ex:

col1  col2  col3
a     1     w
b     2     x
c     3     y
d     5     z


Multi-dimensions arrays sort is resolved by this function in php:

Array Multisort:
http://www.php.net/manual/en/function.array-multisort.php

As advanced tip, you can extend its using by setting witch dimensions you want to sort, leaving the other out. Using the output more as a index to search your data.

The manual can provide enough info to try out this function on our needs. But, if this is not the result you expect, instead you need to create your own sorting method of sorting. Then you can use the:

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

Witch let you create your own method of sorting. You can even extend the multi sort this way.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号