• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Sorting a complex array in php

Barnaby W. Füi

Elite Member
Now I'm alright with php, this seems to me like it's being way more difficult than it should be. I have the following array:

Array
(
[id] => Array
(
[0] => 19
[1] => 20
[2] => 21
[3] => 22
)
[contractlen] => Array
(
[0] => 52
[1] => 12
[2] => 26
[3] => 20
)
[date] => Array
(
[0] => 1049991102
[1] => 1049999348
[2] => 1050010775
[3] => 1050443311
)
)

The real array is bigger but this is good enough to get the point across.

Each of the 4 entries under each sub-array are linked, i.e. all of the [0]'s belong together. I want to be able to sort them, and keep them all linked together, do it case insensitively, AND be able to do it in a function that works independent of how many arrays there are. array_multisort() is about as close as it gets, but it's not case insensitive (but that can be hacked around if all of the alternatives are worse), and it takes a finite number of arguments (one for each array to be sorted). There is no way to make it dynamic when you gotta pass a different number of arguments depending on what you want to do
rolleye.gif
. Right now I have this absolutely hideous if-elseif-elseif statement that covers *most* situations (1-16 or something like that), but that's a horrible horrible way to do it, and I'm just having plenty of problems anyways. The fact that I've been staring at this for a couple of days - both of those days being half unconscious from lack of sleep - does not help. 🙁
 
i think there's an inherant problem in the way your information is stored, the following would be better. it's much prefered for sorting and makes it mucho easy to use a foreach() for... additionall, if you pull items out foa DB it would come out like this. fyi, this is how i store a shopping cart, make it all one large multi-dimensional array bust instead of making each element [0], [1], etc, i might name it the id number to ensure the same item doesn't get added twice, but would replace an existing item. if you want more help or other advice, pm me, i'm more than happy to help those that want to learn how, and not just want an answer

Array
(
[0] => Array
(
[id] => 19
[contractlen] => 52
[date] => 1049991102
)
[1] => Array
(
[id] => 20
[contractlen] => 12
[date] => 1049999348
)
[2] => Array
(
...
)
)
 
That is how I originally was doing it, then I realized that array_multisort() worked alot better with the other style (or so it seemed at the time, at least), but then again array_multisort() isn't exactly working out right now anyways. Hm I suppose you are right, that way the sets of data are intrinsically tied together and sorting them is just a matter of reordering the top level...hmmm... I'll give it a whirl!
 
Worked on switching it over today and it works great 😀

This is how I'm sorting them:

function doSort($sort) {
$sortarr = array();
$newarray = array();

foreach($this->getVisits() as $row=>$arr)
$sortarr[] = $arr[$sort];

natcasesort($sortarr);
$postsort_keys = array_keys($sortarr);

for($i=0; $i<sizeof($postsort_keys); $i++)
$newarray[$i] = $this->_visits[$postsort_keys[$i]];

$this->_visits = $newarray;
}
 
Back
Top