php - Reorder array but don't change keys -
e.g .i have array:
$myarray = ( [0] => 'first', [1] => 'second', [2] => 'third', [3] => 'fourth' );
and need this:
$myarray = ( [0] => 'fourth', [1] => 'third', [2] => 'second', [3] => 'first' );
so, can stored in database in reverse order when compared original array.
i have tried krsort($myarray);
result not want, because creates this:
$myarray = ( [3] => 'fourth', [2] => 'third', [1] => 'second', [0] => 'first' );
and want keys stay in original array.
and problem can not sort values e.g. letters using arsort() etc. because different (different strings, without meaning or order system).
any idea how that?
if want reverse array, use array_reverse
.
$myarray = array_reverse($myarray);
result:
array ( [0] => fourth [1] => third [2] => second [3] => first )
Comments
Post a Comment