Jump to content

Archived

This topic is now archived and is closed to further replies.

AV1611

array()

Recommended Posts

is there a way to sort an array in reverse order

i.e.

array(1,2,3,4,4,5,6)

so

echos out as

6,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]

Share this post


Link to post
Share on other sites
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]


Share this post


Link to post
Share on other sites
Use the function [a href=\"http://www.php.net/rsort\" target=\"_blank\"]rsort()[/a].

Ken

Share this post


Link to post
Share on other sites
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:

adc

into 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];
?>

Share this post


Link to post
Share on other sites
Your original question was
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]is there a way to sort an array in reverse order

i.e.

array(1,2,3,4,4,5,6)

so

echos out as

6,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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
[!--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...

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
[!--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

Share this post


Link to post
Share on other sites
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 :-0

Thanks, 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]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.