Newbie Question: Why Isn"t arsort Sorting?
Newbie Question: Why Isn"t arsort Sorting?
am 27.09.2007 04:09:56 von Vik Rubenfeld
Here's the array I'm trying to sort:
$Differences = Array[3]
Brass = (double) 601.88
Iron = (double) 376.18
Steel = (double) 526.65
Here's the code:
arsort($Differences, SORT_NUMERIC);
It seems as though arsort isn't sorting - the array is in the exact same
order, after arsort runs, as it was before. What am I missing?
Thanks very much in advance to all for any info.
Re: Newbie Question: Why Isn"t arsort Sorting?
am 27.09.2007 07:48:51 von Lars Eighner
In our last episode,
,
the lovely and talented Vik Rubenfeld
broadcast on comp.lang.php:
> Here's the array I'm trying to sort:
> $Differences = Array[3]
> Brass = (double) 601.88
> Iron = (double) 376.18
> Steel = (double) 526.65
Pretty late in September to be doing your first homework assignment, isn't
it? Anyway that 'Array[3]' is nonsense and causes me to suspect that you
did not enter the array properly.
What do you get when you print_r($Differences)?
if you don't get
Array
(
[Brass] => 601.88
[Iron] => 376.18
[Steel] => 526.65
)
you have not entered the array you thought you did.
> Here's the code:
> arsort($Differences, SORT_NUMERIC);
Yes, that would work, if your array is properly entered, but as we said in
the old country "Garbage in, garbage out."
> It seems as though arsort isn't sorting - the array is in the exact same
> order, after arsort runs, as it was before. What am I missing?
You do know arsort gives you descending order. If you want ascending order,
use asort.
> Thanks very much in advance to all for any info.
Here's your answer. Trying going to class once in a while.
#!/usr/local/bin/php
$Differences = array(
Brass => 601.88,
Iron => 376.18,
Steel => 526.65);
print_r($Differences);
/* Array
(
[Brass] => 601.88
[Iron] => 376.18
[Steel] => 526.65
)
is what you get here.
*/
arsort($Differences,SORT_NUMERIC);
print_r($Differences);
/* Array
(
[Brass] => 601.88
[Steel] => 526.65
[Iron] => 376.18
)
is what you get here. Of course there is no reading your mind, and if
you meant a different array or should have used asort, I cannot know that
at this end.
*/
?>
--
Lars Eighner
Countdown: 481 days to go.
What do you do when you're debranded?
Re: Newbie Question: Why Isn"t arsort Sorting?
am 27.09.2007 09:38:33 von Vik Rubenfeld
Lars, you owe me an apology. The confusion is, that I was showing the
contents of the array - not the PHP code that created it, as you
mistakenly assumed. I listed the contents of the array, in the way
they are displayed in the variables window in Zend Studio.
However, running your code, did provide the answer. The order of the
variables as they are displayed in the Zend Studio variables window,
does not change after arsort, even though the print order of the array
has indeed changed.
Re: Newbie Question: Why Isn"t arsort Sorting?
am 27.09.2007 09:59:48 von Lars Eighner
In our last episode,
,
the lovely and talented Vik Rubenfeld
broadcast on comp.lang.php:
> Lars, you owe me an apology. The confusion is, that I was showing the
> contents of the array - not the PHP code that created it, as you
> mistakenly assumed. I listed the contents of the array, in the way
> they are displayed in the variables window in Zend Studio.
> However, running your code, did provide the answer. The order of the
> variables as they are displayed in the Zend Studio variables window,
> does not change after arsort, even though the print order of the array
> has indeed changed.
Another GUI victim! When will the GUI madness stop?
--
Lars Eighner
Countdown: 481 days to go.
What do you do when you're debranded?