Reply to Re: Newbie Question: Why Isn't arsort Sorting?

Your name:

Reply:


Posted by Lars Eighner on 09/27/07 05:48

In our last episode,
<vikr-DA47D3.19095626092007@earthlink.vsrv-sjc.supernews.net>,
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
<?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 <http://larseighner.com/> <http://myspace.com/larseighner>
Countdown: 481 days to go.
What do you do when you're debranded?

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация