471,594 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,594 software developers and data experts.

sorting array...i

Hi,
I am getting confused with sorting arrays...
$arraytest =array(5) { [0]=string(2) "39" [1]=string(2) "44" [2]=>
string(2) "77" [3]=string(3) "150" [4]=string(3) "464" }

why do i get NULL value if i try to sort this array using sort command?

sort($arraytest);
Thanks for your help!
VooDoo
Nov 20 '07 #1
4 1534

"VooDoo" <Vo*********@free.frwrote in message
news:fh**********@reader1.imaginet.fr...
Hi,
I am getting confused with sorting arrays...
$arraytest =array(5) { [0]=string(2) "39" [1]=string(2) "44" [2]=>
string(2) "77" [3]=string(3) "150" [4]=string(3) "464" }

why do i get NULL value if i try to sort this array using sort command?

sort($arraytest);
poorly asked question.

sort() performs a sort on the $array parameter by reference. sort() does NOT
return the sorted version of $array as a result of its operation. so...

<?
$array = array(
'39' ,
'44' ,
'77' ,
'150' ,
'464'
);
sort($array);
echo '<pre>' . print_r($array, true) . '</pre>';
?>

will print the array and show that the values are sorted from lowest to
highest.

btw, leave out the array(5) string(2) [0]=shit. it makes reading the code
a pain in the ass and isn't needed in the least...it doesn't make any impact
on the way php executes that code. if coded like the above, you can easily
see what *actual* values are making up the members of the array...rather
than trying to weed out the other junk surrounding them (like what index it
has or what data-type you are *casting* it to). the extra crap is feckless
and less than useful.
Nov 20 '07 #2
On Tue, 20 Nov 2007 16:32:13 +0100, VooDoo <Vo*********@free.frwrote:
Hi,
I am getting confused with sorting arrays...
$arraytest =array(5) { [0]=string(2) "39" [1]=string(2) "44"[2]=>
string(2) "77" [3]=string(3) "150" [4]=string(3) "464" }

why do i get NULL value if i try to sort this array using sort command?

sort($arraytest);
Probably, because sort() works by reference, not return:

$arraytest = sort($arraytest);
//=>$arraytest = true or false; $arraytest[N] = NULL

sort($arraytest);
//=>$arraytest = sorted array; $arraytest[N] = set value or NULL if
non-existant
--
Rik Wasmus
Nov 20 '07 #3
Ok thanks for your replies.
So the sort its working, I do not need to affect it to a var, just call the
function...

Sorry for the extra crap it was the outpout of a var_dump as i could not
understand what was wrong.
Thanks
Voodoo
Nov 21 '07 #4

"VooDoo" <Vo*********@free.frwrote in message
news:fi**********@reader1.imaginet.fr...
Ok thanks for your replies.
So the sort its working, I do not need to affect it to a var, just call
the function...
right.
Sorry for the extra crap it was the outpout of a var_dump as i could not
understand what was wrong.
Thanks
no problem.
Nov 21 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Paul Kirby | last post: by
7 posts views Thread by Federico G. Babelis | last post: by
3 posts views Thread by SilverWolf | last post: by
7 posts views Thread by Foodbank | last post: by
7 posts views Thread by Kamal | last post: by
5 posts views Thread by lemlimlee | last post: by
5 posts views Thread by jrod11 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.