0

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 keys by 1 or decrease by 1 (acceptable to index is negative int number)

Here is expected result

//after increased by 1
$input = array(
          1=>array('a', 'b'=>array('c')),
          2=>array('b', 'c'=>array('d')),
          3=>array('c', 'd'=>array('e')),
        );

or

//after decreased by 1
$input = array(
          -1=>array('a', 'b'=>array('c')),
          0=>array('b', 'c'=>array('d')),
          1=>array('c', 'd'=>array('e')),
        );

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!

Community
  • 1
  • 1
Telvin Nguyen
  • 3,569
  • 4
  • 25
  • 39
  • So all you want to do is reindex the keys? – aztechy Aug 23 '13 at 02:29
  • @aztechy Your question was not positive since I have talked about it on my question. In most case, I said it was the most ridiculous request that I met, I have tried to avoid to do that as best I can. The requirement is quite complicated and I just say there is problem. – Telvin Nguyen Aug 23 '13 at 02:44
  • It was simply a question, with no malice, to get further clarity at a solution. It looked as simple as updating the first level index, why you want to achieve that, is as you said up to your wishes. Anyways, long story short, no harm was meant by the question. – aztechy Aug 23 '13 at 02:52

2 Answers2

2

This should so the trick.

    // Increase by one
    $input = array_combine(
        array_map(function($value) {
            return $value += 1;
        }, array_keys($input)), $input
    );
maxiscool
  • 537
  • 3
  • 9
  • It is good way to go, what I could see it actually is not different from basic way that Shomz suggested, doesn't it? Btw, I have vote one for your answer and thank you. – Telvin Nguyen Aug 23 '13 at 02:43
  • Either way works. Technically if you use a loop like Shomz suggested you have to assign the results to a new array and then back to the old array if you wish to keep the same variable. Using a loop is probably more readable though. – maxiscool Aug 23 '13 at 02:46
  • No new array needed if you pass by reference. – Shomz Aug 23 '13 at 02:49
1

The easiest way would probably to use foreach and make a new array... like this:

$new = array();
foreach($arr as $k=>$v) {
    $new[$k+1] = $v; // either increase
    $new[$k-1] = $v; // or decrease
}

You can also perform the operation by passing the original array by reference.

Shomz
  • 37,421
  • 4
  • 57
  • 85
  • Thank you, I really don't know this is best way to do because like I said, I consider it as final workaround if there has no other way out. I will mark it answer as soon as I don't get any better solution. – Telvin Nguyen Aug 23 '13 at 02:33
  • 1
    There are many other ways to do this, this one is the most direct and basic. – Shomz Aug 23 '13 at 02:37