1

I have an array like this:

Array(
  'level1' => 'someval',
  'level2' => 'someotherval',
  'level3' => 'thirdval'
)

I want to turn it into this:

Array(
  'someval' => Array(
    'someotherval' => Array(
      'thirdval' => 1
    )
  )
)

Obviously I could build the example above by hand, but I don't know how many levels there will be. And this simple example might seem useless, but there are going to be more values, so there will be multiple arrays inside each of the levels.

2
  • Your questions is a little vague? what are you needing to store inside of the multi-dimensional array? What states to which level an array needs to be nested etc etc? Will that pattern continue or do will some levels contain multiple values? Commented Nov 3, 2011 at 18:20
  • Where does the value of 1 come from? Commented Nov 3, 2011 at 18:31

3 Answers 3

2

This will do it

$array = array(
          'level1' => array(
             'level2' => array(
                 'level3' => 1
              )
           )
         );
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, I mistyped part of my question. Please also see the last paragraph. I can easily build the array in the manner you demonstrated, but I don't know how many levels there will be, so I need to recursively loop through it or something similar.
Not sure what you are looking for, but you might research "pass by reference" and "recursive" function/method calls. That might help your understanding some.
0

Here's my take on it:

function make_multi_level_array($arr) {
        if (count($arr) == 1) return array(array_pop($arr) => 1);
        else {
                $level_key = array_pop(array_reverse($arr));
                $sub_level = make_multi_level_array(
                        array_slice($arr,1,count($arr)-1)       
                );
                return array(
                        $level_key => $sub_level
                );
        }       

}
$arr = array(
   'level1' => 'someval',
   'level2' => 'someotherval',
   'level3' => 'thirdval',
);

var_dump(make_multi_level_array($arr));

Will output this:

array(1) {
  ["someval"]=>
  array(1) {
    ["someotherval"]=>
    array(1) {
      ["thirdval"]=>
      int(1)
    }
  }
}

Also tried other cases like below.

$arr = array(
  'level1' => 'someval',
  'level2' => 'someotherval',
  'level3' => 'thirdval',
  'level4' => 'fourthval'
);

Seems okay:

array(1) {
  ["someval"]=>
  array(1) {
    ["someotherval"]=>
    array(1) {
      ["thirdval"]=>
      array(1) {
        ["fourthval"]=>
        int(1)
      }
    }
  }
}

Comments

0

Do you need something like this?

$levels = array_keys(Array(
  'level1' => 'someval',
  'level2' => 'someotherval',
  'level3' => 'thirdval'
));
$array = Array();
$aux = &$array;
foreach ($levels as $level => $value) {
   if ($aux == 1)
       $aux = array($value => 1);
   $aux = &$aux[$value];
}
var_dump($array);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.