Jump to content


Photo

array()


  • Please log in to reply
9 replies to this topic

#1 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 19 April 2006 - 10:41 PM

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


#2 devofash

devofash
  • Members
  • PipPipPip
  • Advanced Member
  • 81 posts

Posted 19 April 2006 - 11:05 PM

php has its own function called `reverse array` use that ..... something like :

// your array
$array = array (1,2,3,4,5,6);

//apply`reverse array to it`
$reverse = array_reverse($array);




#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 19 April 2006 - 11:53 PM

Use the function [a href=\"http://www.php.net/rsort\" target=\"_blank\"]rsort()[/a].

Ken

#4 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 20 April 2006 - 12:22 AM

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

#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 20 April 2006 - 01:08 AM

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:
<?php
$a = array(1,2,3,4,4,5,6);
echo 'Before rsort: ' . implode(',',$a) . '<br>';
rsort ($a);
echo 'After rsort: ' . implode(',',$a);
?>

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:
<php
$a2 = array('a','d','c');
echo 'Before reverse: ' . implode('',$a2) . '<br>';
$a2r = array_reverse($a2);
echo 'After reverse: ' . implode('',$a2r);
?>

Ken


#6 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 20 April 2006 - 01:18 AM

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?
~ D Kuang

#7 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 20 April 2006 - 01:39 AM

[!--quoteo(post=366678:date=Apr 19 2006, 08:18 PM:name=poirot)--][div class=\'quotetop\']QUOTE(poirot @ Apr 19 2006, 08:18 PM) View Post[/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...



#8 litebearer

litebearer
  • Members
  • PipPipPip
  • Advanced Member
  • 2,357 posts
  • Locationwhite lake michigan

Posted 20 April 2006 - 02:05 AM

Just a crazy thought....

<?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>";
}
?>

Lite...

all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture

#9 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 20 April 2006 - 02:37 AM

[!--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:
<?php
$a[] = 'a';
$a[] = 'x';
$a[] = 'n';
echo '<pre>' . print_r($a,true) . '</pre>';
?>
and this:
<?php
$a = array('a','x','n');
echo '<pre>' . print_r($a,true) . '</pre>';
?>
will produce the same results.
As will this:
<?php
$a[0] = 'a';
$a[1] = 'x';
$a[2] = 'n';
echo '<pre>' . print_r($a,true) . '</pre>';
?>

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:
<?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];
?>
It should do what you want.

Ken

#10 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 20 April 2006 - 12:37 PM

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) View Post[/div][div class=\'quotemain\'][!--quotec--]
What do you mean a "keyed array"? All arrays have keys. They are either numeric or strings.

This:
<?php
$a[] = 'a';
$a[] = 'x';
$a[] = 'n';
echo '<pre>' . print_r($a,true) . '</pre>';
?>
and this:
<?php
$a = array('a','x','n');
echo '<pre>' . print_r($a,true) . '</pre>';
?>
will produce the same results.
As will this:
<?php
$a[0] = 'a';
$a[1] = 'x';
$a[2] = 'n';
echo '<pre>' . print_r($a,true) . '</pre>';
?>

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:
<?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];
?>
It should do what you want.

Ken
[/quote]





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users