0

I am developing a new website, and I have a quetion.

Input array:

Array ( [1319] => ####,[1316] => ###)

I have an array and I want to revese him, after the reverse the array would be like this:

Expected output:

Array ( [1316] => ###,[1319] => ####)

but when i'm using array_reverse function, it doesnt work for me, I got this array:

Array ( [0] => ###,[1] => ####)

Why it is happen?

1
  • You can sort it by key with ksort. Commented Jun 3, 2017 at 9:33

2 Answers 2

2

For preserving keys you just pass second parameter to true in array_reverse.

Try this code snippet here

$array=Array ( 1319 => "####",1316 => "###");
print_r(array_reverse($array,true));
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it is the best way!
1

you can try this:

$a = []; //your array

$keys = array_keys($arr);
$values = array_values($arr);

$rv = array_reverse($values);

$newArray = array_combine($keys, $rv);

2 Comments

Thank you! it's working for me. Buy can you explaing me why it doesnt work with only the array_reverse fucntion?
Glad to help you! @Jack accept the answer when you can to help other guys with your same problem!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.