...

View Full Version : Help me on how to echo array_reverse in php



kingot
01-11-2012, 12:36 PM
Hi ,
I am writing a code which i want to echo it out and see the output rather than print_r,

can someone help me out
Here is the code


[<?php]
$array=array('Red','Green','Yellow');

$array_r=array_reverse($array);
print_r($array_r);

[?>]


Thanks
Clement Ose

Satoius
01-11-2012, 03:24 PM
I use a for loop but I'm a C progarmmer. If there is an easier way I'd like to know. But this should work...

$count = count($array_r);

for($i = 0; $i = ($count - 1); $i++){
echo $array_r[$i];
}

kbluhm
01-11-2012, 03:34 PM
http://www.php.net/foreach

foreach() loop would work as well:


foreach ( $array as $value )
{
echo $value, "<br /><br />\n";
}

foreach ( $array as $key => $value )
{
echo $key , "<br />\n";
echo $value, "<br /><br />\n";
}

_Aerospace_Eng_
01-11-2012, 03:35 PM
Along the lines of the for loop there are multiple ways to do this e.g. using foreach (as shown), a while loop

while(list($key,$value) = each($array_r))
{
echo $value.'<br>';
}
each returns key=>value pairs which is why I have them in the for loop.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum