Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Feb 2011
    Posts
    114
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Help me on how to echo array_reverse in php

    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
    Code:
     
    [<?php]
     $array=array('Red','Green','Yellow');
      
      $array_r=array_reverse($array);
      print_r($array_r); 
    
    [?>]

    Thanks
    Clement Ose

  • #2
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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];
    }

  • #3
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    http://www.php.net/foreach

    foreach() loop would work as well:
    PHP Code:
    foreach ( $array as $value )
    {
        echo 
    $value"<br /><br />\n";
    }

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


  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Along the lines of the for loop there are multiple ways to do this e.g. using foreach (as shown), a while loop
    PHP Code:
    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.
    ||||If you are getting paid to do a job, don't ask for help on it!||||


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •