0

I'm looking for some simple solution (1 line) to increment all the keys +1 in an multi array.

    Array (
       0 => Array (
                   7 => 'foo',
                   13 => 'foo'
                  ),
       1 =>  Array (
                   2 => 'foo',
                   25 => 'foo'
                  ),
       4 =>  Array (
                   24 => 'foo'
                  )
)

RESULT ARRAY

    Array (
       1 => Array (
                   8 => 'foo',
                   14 => 'foo'
                  ),
       2 =>  Array (
                   3 => 'foo',
                   26 => 'foo'
                  ),
       5 =>  Array (
                   25 => 'foo'
                  )
)
Online
  • 49
  • 1
  • 3
  • Any particular reason why? – Mark Baker Sep 05 '16 at 23:32
  • http://stackoverflow.com/questions/12715514/how-to-increase-by-1-all-keys-in-an-array –  Sep 05 '16 at 23:35
  • To make it more readable for the end user. I know, I can increment it when I show the info, but not is the point. – Online Sep 05 '16 at 23:38
  • Well something like `array_combine(array_map(function ($key) { return ++$key; }, array_keys($data)), $data)` would be a starting point, but you'd have to do it recursively as well; it's probably a lot easier to do it when you display the info – Mark Baker Sep 05 '16 at 23:43
  • 1
    Can't understand why this question is downvoted. It's a simple and indeed interesting question. There should be no shaming on such questions regardless of the purpose. We don't know if it's a theoretical or practical problem here. We should just help solving it. – Arvid May 25 '20 at 13:17

2 Answers2

3
$data = [
       0 => [7 => 'foo', 13 => 'foo'],
       1 => [2 => 'foo', 25 => 'foo'],
       4 => [24 => 'foo'],
];

// top level
$data = array_combine(
    array_map(function ($key) { return ++$key; }, array_keys($data)),
    $data
);
// then the elements (use array_walk_recursive() if you have more levels)
array_walk(
    $data,
    function(&$data) {
        $data = array_combine(
            array_map(function ($key) { return ++$key; }, array_keys($data)),
            $data
        );
    }
);
var_dump($data);

Demo

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
0

I'm not sure about a one-liner, especially since you have a multidimensional array. However, this is how I would do it. Honestly, all that one-liner elegance will just make things more complicated for you anyway. It's better to go with readable code in my humble opinion.

// Example array
$array = array (
      0 => array (
            7 => 'foo',
            13 => 'foo'
      ),
      1 =>  array (
            2 => 'foo',
            25 => 'foo'
      ),
      4 =>  array (
            24 => 'foo'
      )
);

// My solution to increment each key and nested key
$new = array();
foreach($array as $key => $value) {
      foreach($value as $inkey => $inval) {
            $value[$inkey+1] = $inval;
            unset($value[$inkey]);
      }
      $new[$key+1] = $value;
}
$array = $new;

// Display the array
echo '<pre>'; print_r($array); echo '<pre/>';
Anthony
  • 221
  • 2
  • 10