[UPHPU] easy asort() question
design5279 at gmail.com
Wed Jun 22 10:03:48 MDT 2005
On 6/21/05, John M <johnm_ul at yahoo.com> wrote:
> I'm going through some examples on the different sort
> functions and asort has me a bit confused. It's not
> sorting like I think it should be.
> I've read...
> ...and the way I understand it is that it is supposed
> to be sorted by value and not index.
Your gradestudent array is sorting properly by value. In your code, the
gradestudent key (or index) is numerical like Mac said because adding
something to $arrayname adds only to the value and the index is assigned
automatically (in other words, you are assigning $math$student lumped
together into one value). Also, you are sorting the gradestudent array, but
you are printing out something different (i.e. you gave the array values
$math$student, but you print out $student then $math: echo
"<br>$student[$index] - $math[$index]";). It sounds like you really want to
be sorting by $student[$index].$math[$index]. If you just switch $math and
$student in your array, it should sort the way you want.
You should also print out the arrays before and after the asort() like Mac
suggested so you can visualize them. If you put <pre> tags around the
print_r() output, you can see them more easily.
echo "This is the gradestudent array <i>before</i> asort().<br /><pre>";
echo "This is the gradestudent array <i>after</i> asort().<br /><pre>";
$arrayname[key] = value;
$arrayname = "bar"; will make $arrayname = bar ... key is 0
$arrayname = array("foo" => "bar"); will make $arrayname[foo] = bar ... key
In this case, since you want to sort by value, it doesn't really matter what
your key is.
More information about the UPHPU