Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

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)
share|improve this question
    
possible duplicate of Creating a multidimensional array from row – Marc B Aug 7 '13 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. – Dirk McQuickly Aug 7 '13 at 16:49
    
no not a copy my request is slightly but different :) – 4485670 Aug 8 '13 at 8:47

5 Answers 5

up vote 1 down vote accepted
<?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.

share|improve this answer
    
Thanks this is what I was looking for thanks! – 4485670 Aug 8 '13 at 8:44

Try this:

$out = array();
$cur = &$out;
foreach ($array as $value) {
    $cur[$value] = array();
    $cur = &$cur[$value];
}
$cur = null;

print_r($out);

Grabbed it from another post.

share|improve this answer
    
How about linking to the post so that I can give it an upvote? – Waldermort Jun 12 '14 at 15:06

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);
}
share|improve this answer
    
$array = array( 0 => 'folder', 1 => 'subfolder', ..., x => 'file.txt' ); is what I need it for, hope that is more clear – 4485670 Aug 8 '13 at 8:53

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
                        (
                        )

                )

        )

)
share|improve this answer
    
thanks for the answer, x stands for x-number (any number) – 4485670 Aug 8 '13 at 8:51

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.

share|improve this answer
    
hi thanks for your answer, the last key - value pair is indeed treated differently – 4485670 Aug 8 '13 at 8:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.