Re Order an Array -- help please!

Discussion in 'PHP' started by phantom, Dec 2, 2008.

  1. #1
    I need help reordering this data in the var $favsget

    $favsget = '123,456,768,55,66,55,';

    $favlist = explode(",",$favsget);
    foreach ( $favlist as $name){

    //Now I need to reverse the order and it needs to be exactly from last to first where last = 55 and first is 123



    then do other stuff after I get it reordered


    Can anyone give me some insight how to do this?


    Thanks in advance!
     
    phantom, Dec 2, 2008 IP
  2. misbah

    misbah Active Member

    Messages:
    265
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    68
    #2
    you can try this
    $favsget = '123,456,768,55,66,55';
    $favlist = explode(',', $favsget);
    $newArray = array();
    $len = count($favlist) - 1;
    for($i = $len; $i >= 0; $i--)
    {
    $newArray[] = $favlist[$i];
    }
    
    PHP:
     
    misbah, Dec 2, 2008 IP
  3. djzmo

    djzmo Active Member

    Messages:
    165
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #3
    
    $favsget = '123,456,768,55,66,55,';
    $favlist = explode(",",$favsget);
    $total = count($favlist) - 1;
    foreach ($favlist as $key => $value)
    {
      $favlist_reversed[$total] = $favlist[$key];
      $total--;
    }
    
    PHP:
     
    djzmo, Dec 2, 2008 IP
  4. phantom

    phantom Well-Known Member

    Messages:
    1,509
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    140
    #4
    Ok thanks guys I will try them both!
     
    phantom, Dec 2, 2008 IP