3

I have an array like this:

<?php
     $array = array( 0 => 'foo', 1 => 'bar', ..., x => 'foobar' );
?>

What is the fastest way to create a multidimensional array out of this, where every value is another level? So I get:

array (size=1)
 'foo' => 
   array (size=1)
    'bar' => 
      ...
      array (size=1)
        'x' => 
          array (size=1)
            0 => string 'foobar' (length=6)
4
  • possible duplicate of Creating a multidimensional array from row Commented Aug 7, 2013 at 16:21
  • it seems like the best answer is to be found here: stackoverflow.com/questions/8956222/… as there are two guys that copy-pasted their answer from here. Must be good then. Commented Aug 7, 2013 at 16:49
  • no not a copy my request is slightly but different :) Commented Aug 8, 2013 at 8:47
  • Why for all numeric indices, the value is the one that ends up as a key in the result array, but for the last one it's the key (x) that ends up as key in the result array? Commented Jan 15, 2023 at 8:58

4 Answers 4

1
<?php
$i = count($array)-1;
$lasta = array($array[$i]);
$i--;    
while ($i>=0)
{
    $a = array();
    $a[$array[$i]] = $lasta;
    $lasta = $a;
    $i--;
}
?>

$a is the output.

Sign up to request clarification or add additional context in comments.

Comments

0

What exactly are you trying to do? So many arrays of size 1 seems a bit silly.

you probably want to use foreach loop(s) with a key=>value pair

foreach ($array as $k=>$v) {
  print "key: $k  value: $v";
}

You could do something like this to achieve the array you asked for:

$newArray = array();
for ($i=count($array)-1; $i>=0; $i--) {
  $newArray = array($newArray[$i]=>$newArray);
}

1 Comment

$array = array( 0 => 'folder', 1 => 'subfolder', ..., x => 'file.txt' ); is what I need it for, hope that is more clear
0

I'm confused about what you want to do with non-numeric keys (ie, x in your example). But in any case using array references will help

$array = array( 0 => 'foo', 1 => 'bar',  x => 'foobar' );


$out = array();
$curr = &$out;

foreach ($array as $key => $value) {
    $curr[$value] = array(); 
    $curr = &$curr[$value];
}

print( "In: \n" );
print_r($array);
print( "Out : \n" );
print_r($out);

Prints out

In:
Array
(
    [0] => foo
    [1] => bar
    [x] => foobar
)
Out :
Array
(
    [foo] => Array
        (
            [bar] => Array
                (
                    [foobar] => Array
                        (
                        )

                )

        )

)

Comments

0

You can use a recursive function so that you're not iterating through the array each step. Here's such a function I wrote.

function expand_arr($arr)
{
    if (empty($arr))
        return array();

    return array(
        array_shift($arr) => expand_arr($arr)
    );
}

Your question is a little unclear since in your initial statement you're using the next value in the array as the next step down's key and then at the end of your example you're using the original key as the only key in the next step's key.

1 Comment

hi thanks for your answer, the last key - value pair is indeed treated differently

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.