Log in

View Full Version : Help sorting this array


Dean C
10-28-2004, 06:20 PM
Array
(
[0] => Array
(
[13] => Array
(
[0] => Suzukis release a new bike yada
[1] => 6
)

)

[1] => Array
(
[1] => Array
(
[0] => Test Title
[1] => 8
)

)

[2] => Array
(
[2] => Array
(
[0] => BiZ article testing
[1] => 19
)

)

[3] => Array
(
[2] => Array
(
[0] => biz starboyz test234
[1] => 20
)

)

[4] => Array
(
[2] => Array
(
[0] => Beat the Winter Bugs - Cold and Flu
[1] => 22
)

)

[5] => Array
(
[1] => Array
(
[0] => Test Name
[1] => 26
)

)

[6] => Array
(
[2] => Array
(
[0] => Another test
[1] => 27
)

)

)


Is it possible to sort this array by the second key down instead of the first. I.e. the keys which contain the values 13, 1, 2, 2, 2, 1, 2

Andreas
10-28-2004, 06:46 PM
function cmp($a, $b) {
$keys_a = array_keys($a);
$keys_b = array_keys($b);
if ($keys_a[0] == $keys_b[0]) return 0;
return ($keys_a[0] < $keys_b[0]) ? -1 : 1;
}

usort($array, 'cmp');


Smth. like this might work.

Dean C
10-28-2004, 06:50 PM
No luck Kirby :(

Andreas
10-28-2004, 07:19 PM
Hmm, running this code gives me


Array
(
[0] => Array
(
[1] => Array
(
[0] => Test Title
[1] => 8
)

)

[1] => Array
(
[2] => Array
(
[0] => Another test
[1] => 27
)

)

[2] => Array
(
[2] => Array
(
[0] => Beat the Winter Bugs - Cold and Flu
[1] => 22
)

)

[3] => Array
(
[2] => Array
(
[0] => BiZ article testing
[1] => 19
)

)

[4] => Array
(
[2] => Array
(
[0] => biz starboyz test234
[1] => 20
)

)

[5] => Array
(
[13] => Array
(
[0] => Suzukis release a new bike yada
[1] => 6
)

)

)


Isn't that what you want?

Dean C
10-28-2004, 07:37 PM
Yeah but it didn't work when I did. I've just changed my code anyway, thanks for the help Kirby :)