Forum Moderators: coopster

Message Too Old, No Replies

Multidimensional Arrays - sorting

asort array multidimensional

         

AndieR

8:48 pm on Dec 26, 2007 (gmt 0)

10+ Year Member



Hello to all,

I am starting with multidimensional arrays and I can't seem to picture how to build and sort the following concept:

I have two variables (the zip and the distance in miles) I am pulling in a loop to build an array... how to go about sorting it out now by $miles?

Now what I am wondering is... is this correct syntax? Does it make any sense at all? I am really confused.

I have this example online but I can't seem to make it work:

if(($miles <= $distance)){
$data[$s] = array('zip' => $zip1[0], 'miles' => $miles);
$s++;
}

foreach ($data as $key => $row) {
$zip[$key] = $row['zip'];
$miles[$key] = $row['miles'];
}

$new = array_multisort($zip, SORT_DESC, $miles, SORT_ASC, $data);

print_r ($new);

Honestly I can't tell whether this is the right way to go as it's a first for me but I'd sure appreciate any tips/help!

Thanks :)
Andie

AndieR

9:36 pm on Dec 26, 2007 (gmt 0)

10+ Year Member



:P I found a way around! Associative arrays :)

if(($miles <= $distance)){
$narray["$zip1[0]"]="$red";
$s++;
}

asort($narray);

foreach($narray as $key => $value)
{
print $key . " = " . $value . "<br />";
}

Thanks!
Andie