BGTabulation BGTabulate
BGTabulation BGTabulate

Reputation: 1737

How to reverse array key without affecting the values

I have an array array(1=>'randstring1', 2=>'randstring2', 3=>'randstring3', 4=> 'randstring4') I want to reverse or sort the key from 4 to 1 while preserving the value so it would be like

Array(
   [4] => randstring1
   [3] => randstring2
   [2] => randstring3
   [1] => randstring4
)

Upvotes: 1

Views: 528

Answers (1)

Scuzzy
Scuzzy

Reputation: 12332

A combination of array_combine, array_reverse and array_keys should do the trick.

$newArray = array_combine( array_reverse( array_keys( $array ) ), $array );

Upvotes: 5

Related Questions