Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse array in php

Tags:

arrays

php

array(7) {
  [0]=> array(2) { ["id"]=> string(1) "9"  ["roi"]=> float(0)    }
  [1]=> array(2) { ["id"]=> string(1) "1"  ["roi"]=> float(0)    }
  [2]=> array(2) { ["id"]=> string(2) "10" ["roi"]=> float(0)    }
  [3]=> array(2) { ["id"]=> string(2) "14" ["roi"]=> float(0)    }
  [4]=> array(2) { ["id"]=> string(1) "4"  ["roi"]=> float(0)    }
  [5]=> array(2) { ["id"]=> string(1) "5"  ["roi"]=> float(141)  }
  [6]=> array(2) { ["id"]=> string(1) "6"  ["roi"]=> float(2600) }
}

I would just like to reverse this, so id 6 (with roi of 2600) comes first in the array etc.

How can I do this? array_reverse() and rsort() does not work in this case

like image 624
Karem Avatar asked Apr 05 '12 12:04

Karem


2 Answers

http://php.net/manual/en/function.array-reverse.php:

$newArray = array_reverse($theArray, true);

The important part is the true parameter, which preserves the keys.

Not convinced? You can see it in action on this codepad exampole.

like image 168
PeeHaa Avatar answered Oct 20 '22 03:10

PeeHaa


foreach($array as $arr){

  array_unshift($array, $arr); 
  array_pop($array);

}
like image 21
user933791 Avatar answered Oct 20 '22 05:10

user933791