AV1611 Posted April 19, 2006 Share Posted April 19, 2006 is there a way to sort an array in reverse orderi.e.array(1,2,3,4,4,5,6)soechos out as6,5,4,4,3,2,1???This doesn't work, because it doesn't have a key in the array, I guess...[code]<?php$s[]="a";$s[]="b";$s[]="d";echo $s[0].$s[1].$s[2];array_reverse($s);echo "</br>";echo $s[0].$s[1].$s[2];?>[/code] Quote Link to comment Share on other sites More sharing options...
devofash Posted April 19, 2006 Share Posted April 19, 2006 php has its own function called `reverse array` use that ..... something like :[code]// your array$array = array (1,2,3,4,5,6);//apply`reverse array to it`$reverse = array_reverse($array);[/code] Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 19, 2006 Share Posted April 19, 2006 Use the function [a href=\"http://www.php.net/rsort\" target=\"_blank\"]rsort()[/a].Ken Quote Link to comment Share on other sites More sharing options...
AV1611 Posted April 20, 2006 Author Share Posted April 20, 2006 rsort does not work. I don't want reverse highest by lowest. I want the reverse the order of the random characters in the array. (I am making a line-graph, but the numbers are backwards... if I reverse the array, I'll reverse the graph...)(BTW- reverse-array only works with a keyed array)it turned this:adcinto this:dca<?php$s[]="a";$s[]="d";$s[]="c";echo $s[0].$s[1].$s[2];rsort($s);echo "</br>";echo $s[0].$s[1].$s[2];?> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 20, 2006 Share Posted April 20, 2006 Your original question was[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]is there a way to sort an array in reverse orderi.e.array(1,2,3,4,4,5,6)soechos out as6,5,4,4,3,2,1[/quote]This does that:[code]<?php$a = array(1,2,3,4,4,5,6);echo 'Before rsort: ' . implode(',',$a) . '<br>';rsort ($a);echo 'After rsort: ' . implode(',',$a);?>[/code]You've changed your query in mid question to:[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]I want the reverse the order of the random characters in the array. [/quote]This code does that:[code]<php$a2 = array('a','d','c');echo 'Before reverse: ' . implode('',$a2) . '<br>';$a2r = array_reverse($a2);echo 'After reverse: ' . implode('',$a2r);?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
poirot Posted April 20, 2006 Share Posted April 20, 2006 C'mon, there are several array sorting functions:arsort(), asort(), ksort(), sort(), and usort()[a href=\"http://www.php.net/manual/en/ref.array.php\" target=\"_blank\"]http://www.php.net/manual/en/ref.array.php[/a]One of them should do it, don't you think? Quote Link to comment Share on other sites More sharing options...
AV1611 Posted April 20, 2006 Author Share Posted April 20, 2006 [!--quoteo(post=366678:date=Apr 19 2006, 08:18 PM:name=poirot)--][div class=\'quotetop\']QUOTE(poirot @ Apr 19 2006, 08:18 PM) [snapback]366678[/snapback][/div][div class=\'quotemain\'][!--quotec--]C'mon, there are several array sorting functions:arsort(), asort(), ksort(), sort(), and usort()[a href=\"http://www.php.net/manual/en/ref.array.php\" target=\"_blank\"]http://www.php.net/manual/en/ref.array.php[/a]One of them should do it, don't you think?[/quote]non of what you listed does a simple reversal of the array. see my initial post. if you have a keyed array, they will work fine. I don't have a keyed array, so they don't work... Quote Link to comment Share on other sites More sharing options...
litebearer Posted April 20, 2006 Share Posted April 20, 2006 Just a crazy thought....[code]<?PHP$old_array[] = "a";$old_array[] = "c";$old_array[] = "b";$old_array[] = "d";$count = count($old_array);$i2= $count-1;$i = 0;for ($i=0;$i<$count;$i++) { $new_array[$i] = $old_array[$i2]; $i2=$i2-1;}$i=0;for ($i=0;$i<$count;$i++) { echo $i . " = " . $new_array[$i] . "<br>";}?>[/code]Lite... Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 20, 2006 Share Posted April 20, 2006 [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]non of what you listed does a simple reversal of the array.see my initial post. if you have a keyed array, they will work fine. I don't have a keyed array,so they don't work...[/quote]What do you mean a "keyed array"? All arrays have keys. They are either numeric or strings.This:[code]<?php$a[] = 'a';$a[] = 'x';$a[] = 'n';echo '<pre>' . print_r($a,true) . '</pre>';?>[/code]and this:[code]<?php$a = array('a','x','n');echo '<pre>' . print_r($a,true) . '</pre>';?>[/code]will produce the same results.As will this:[code]<?php$a[0] = 'a';$a[1] = 'x';$a[2] = 'n';echo '<pre>' . print_r($a,true) . '</pre>';?>[/code]Did you try the script snippets I posted 4 posts back?The reason your first attempt at using the reverse_array() didn't work was that the function returns an array, it doesn't reverse it in place. You didn't save the reversed array anywhere so you could echo the contents. You just echoed the original elements again.If you fix it like this:[code]<?php$s[]="a";$s[]="b";$s[]="d";echo $s[0].$s[1].$s[2];$rs = array_reverse($s);echo "<br />";echo $rs[0].$rs[1].$rs[2];?>[/code]It should do what you want.Ken Quote Link to comment Share on other sites More sharing options...
AV1611 Posted April 20, 2006 Author Share Posted April 20, 2006 Ken and Lite:Apologies!Lite, I already came up with your method and was going to do it that way, but It was bothering me that Ken would come up with a wrong answer, so I re-read his post, and realized I misread what he did... so he was right all along :-0Thanks, all![!--quoteo(post=366701:date=Apr 19 2006, 09:37 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 19 2006, 09:37 PM) [snapback]366701[/snapback][/div][div class=\'quotemain\'][!--quotec--]What do you mean a "keyed array"? All arrays have keys. They are either numeric or strings.This:[code]<?php$a[] = 'a';$a[] = 'x';$a[] = 'n';echo '<pre>' . print_r($a,true) . '</pre>';?>[/code]and this:[code]<?php$a = array('a','x','n');echo '<pre>' . print_r($a,true) . '</pre>';?>[/code]will produce the same results.As will this:[code]<?php$a[0] = 'a';$a[1] = 'x';$a[2] = 'n';echo '<pre>' . print_r($a,true) . '</pre>';?>[/code]Did you try the script snippets I posted 4 posts back?The reason your first attempt at using the reverse_array() didn't work was that the function returns an array, it doesn't reverse it in place. You didn't save the reversed array anywhere so you could echo the contents. You just echoed the original elements again.If you fix it like this:[code]<?php$s[]="a";$s[]="b";$s[]="d";echo $s[0].$s[1].$s[2];$rs = array_reverse($s);echo "<br />";echo $rs[0].$rs[1].$rs[2];?>[/code]It should do what you want.Ken[/quote] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.