fork download
  1. <?php
  2. $array = [
  3. 'hello' => 'world',
  4. 'doing' => [
  5. 'hello' => 'universe',
  6. 'going' => [
  7. 'hello' => 'existence'
  8. ],
  9. 'moving' => [
  10. 'answer' => 42,
  11. ]
  12. ]
  13. ];
  14. function levelUp(&$array, $level = 1) {
  15. $array['id'] = $level;
  16. foreach($array as $key => &$value) {
  17. if(is_array($value)) {
  18. levelUp($value, $level + 1);
  19. }
  20. }
  21. }
  22. levelUp($array);
  23.  
  24. var_dump($array);
  25. // your code goes here
Success #stdin #stdout 0.02s 23444KB
stdin
Standard input is empty
stdout
array(3) {
  ["hello"]=>
  string(5) "world"
  ["doing"]=>
  array(4) {
    ["hello"]=>
    string(8) "universe"
    ["going"]=>
    array(2) {
      ["hello"]=>
      string(9) "existence"
      ["id"]=>
      int(3)
    }
    ["moving"]=>
    array(2) {
      ["answer"]=>
      int(42)
      ["id"]=>
      int(3)
    }
    ["id"]=>
    int(2)
  }
  ["id"]=>
  int(1)
}