Thursday, 22 August 2013

Reindex array with increase and decreasement index

Reindex array with increase and decreasement index

First, I have to list what I have found
How to Remove Array Element and Then Re-Index Array?
PHP reindex array?
They did not make sense in my case. I have fallen into ridiculous request
and I've force to find the way out, then please don't ask me why I want to
do that.
I have below array
$input = array(
0=>array('a', 'b'=>array('c')),
1=>array('b', 'c'=>array('d')),
2=>array('c', 'd'=>array('e')),
);
I want to increase all of key by 1 or decrease by 1 (acceptable to index
is negative int number) The closet answer I got here from raina77ow in the
question How to increase by 1 all keys in an array?
$arr = array_flip(array_map(function($el){ return $el + 1; },
array_flip($arr)));
But it just works with simple array key pairs, if array value is other
array instead of a string or integer, it would raise the exception
array_flip(): Can only flip STRING and INTEGER values!
The thing what I could think is handling the array to swap roster
manually, it would be the final way if there were not any other
workaround.
Any help is appreciated!

No comments:

Post a Comment