Forum Moderators: coopster

Message Too Old, No Replies

Return of [YAMDAP] Yet Another Multi Dimensional Array Problem

This one is much more complex than before, not for the faint of heart

         

haryanto

4:28 am on Aug 30, 2004 (gmt 0)

10+ Year Member



Hi guys,

I posted a Yet Another Multi Dimensional Array Problem[YAMDAP] a while ago which was solved by JPJones.I decided to come back with another one which is harder than the first version. I cracked my brain for 7 hrs reading up and experimenting with this but I couldn't find a solution. Hope there is a PHP guru out there to come help me out before I lose all my hair.

OK here is the array.

print_r ($fellas) gives me this:

Array
(
[Fat_man_Joe] => Array
(
[0] => burger
[1] => 700
)

[Skinny_Victor] => Array
(
[0] => salad
[1] => 65
)

[Fat_man_John] => Array
(
[0] => Cakes
[1] => 685
)
}

How do we do a function to rearrange the array so that the output of the array is based on the weight of a person?
e.g. Skinny_Victor,Fat_man_John then Fat_man_Joe

I need the array to output:

Array
(
[Skinny_Victor] => Array
(
[0] => salad
[1] => 65
)

[Fat_man_John] => Array
(
[0] => Cakes
[1] => 685
)
[Fat_man_Joe] => Array
(
[0] => burger
[1] => 700
)
}

Warboss Alex

5:42 am on Aug 30, 2004 (gmt 0)

10+ Year Member



Ta-da! (didn't see what all the fuss was about..)

Change to SORT_DESC if desired, adjust values to taste, and serve chilled.

Cheers,
Alex ...

<pre>

<?php

$a = array();

$a['Fat Man Joe'] = array('burger', '700');
$a['Skinny Victor'] = array('salad', '65');
$a['Fat Man John'] = array('cakes', '685');

print_r($a);

foreach ($a as $key => $row) {
$weight[$key] = $row[1];
}

array_multisort($weight, SORT_ASC, $a);

print_r($a);

?>

</pre>

jollymcfats

3:52 pm on Aug 30, 2004 (gmt 0)

10+ Year Member



You can also sort the array in-place with the user sort functions (usort, uksort, uasort).

With these sorts, the sorting decisions are made by a function you provide. The function is passed values from the array, two at a time, and you compare the two however you like and return the result.

With a "flat" array, say,

array('dog', 'cat')
, your comparison function is passed pairs of array values:
my_compare('dog', 'cat')
.

With a multi-dimensional array it's the same, but in this case the values of the array are arrays themselves. You can't compare them directly, but your function can easily compare values of the array subscripts.

It sounds more complicated than it is. For the people array, this little function will sort by weight:


function cmp ($a,$b) {
if ($a[1] == $b[1]) return 0;
return ($a[1] < $b[1])? -1 : 1;
}
uasort($array, 'cmp');

In the php manual, the

usort
function has the best documentation. In this case, you actually need
uasort
, but that function is only minimally documented.

haryanto

6:04 pm on Aug 30, 2004 (gmt 0)

10+ Year Member



Jolly, Warboss Alex's function works.
I was amazed by how knowledgable people are in WebmasterWorld.

Your function looks more complicated. Is it more flexible?

jollymcfats

6:16 pm on Aug 30, 2004 (gmt 0)

10+ Year Member



Yes, the user sort is much more flexible, and doesn't require you to make copies of the arrays.

With a user sort, you can sort on as many criteria as you want. For example, "weight" being equal, you could then further sort on "food type":


function cmp ($a,$b) {
// "weight" equal? sort on "food types" in index 0.
if ($a[1] == $b[1]) {
return strcmp($a[0], $b[0]);
}
// otherwise just sort by "weight"
return ($a[1] < $b[1])? -1 : 1;
}
uasort($array, 'cmp');