Php dynamic array name

I am trying to assign values of array by dynamic name I am trying following

$arr = array();

$path = 'arr'."['item']['abc']";

${$path} = array(
       'name'=>'somename',
       'other'=>'...'
);

isn't ${$path} => $arr['item']['abc'];

also I tried $$path which should evaluated as $arr['item']['abc']; but none of them working

Updated ->


What I am trying to do is store files and folder in array I have directory structure like this

(this is amazon aws s3 object keys)

   aaa/aaab/ 
   aaa/a.png 
   aaa/abc/one.png 

   abb/a/
   abb/some/
   abb/some/ac.png

now what I am trying to do is store those items in array

I broke down those keys into this string like

 ['aaa']['abc']

now what I want to get is

[aaa]{
    [aaab]{}
    [abc]{
        [0]{one.png}
    }
    [aaab]{}
}

its just for illustration


ANSWERS:


A less weired approach than variable variables would be to build a proper structure:

<?php

$arr = array();

$loc_arr = array(
   'item' => array(
           'abc',
           'www',
           'ccc'),
   'other item' => array(
           'other index'
   )
);

foreach($loc_arr as $item => $scndLevel){
    foreach ( $scndLevel as $level) {
        $arr[$item][$level] = array(
           'name'=>'somename',
           'other'=>'...'
        );
    }
}

print_r($arr);

this prints:

Array
(
    [item] => Array
        (
            [abc] => Array
                (
                    [name] => somename
                    [other] => ...
                )    
            [www] => Array
                (
                    [name] => somename
                    [other] => ...
                )   
            [ccc] => Array
                (
                    [name] => somename
                    [other] => ...
                )    
        )   
    [other item] => Array
        (
            [other index] => Array
                (
                    [name] => somename
                    [other] => ...
                )    
        )    
)

UPDATE:

You should consider a recursional function when traversing directories rather than a procedural approach:

Pseudo code:

dir = array();

function traverse(dir)
{
    foreach ( direntry as filename) {
        if ( filename is a sub directory ) {
            dir[filename] = array();
            traverse(dir[filename]);
        } else {
            dir[] = filename;
        }

}
dir['/'] = array();
traverse(dir['/']);

That's only to give you an idea and a starting point, this is not working code.


Referring to your updated codpad, there are multiple possible ways to write this without using variable variables.

Here's one possible option:

<?php

$arr = array();

$loc_arr = array('item' => array('abc', 'www', 'ccc'));

foreach($loc_arr as $itemKey=>$items){
  foreach($items as $subitem) {
    $arr[$itemKey][$subitem] = array(
       'name'=>'somename',
       'other'=>'...'
    );
  }
}

As I mentioned in the comments, it's very difficult to know how suitable this specific solution would be to your particular use-case, but I think it should demonstrate clearly that there shouldn't really be any need for variable variables in this kind of context.

Variable variables should be thought of much like eval(): Avoid using them wherever possible. If you find yourself wanting to use them, then you're probably approaching things from the wrong angle. They exist because they can be useful in some occasional cases, but those cases are very rare. I've been writing PHP as a professional developer for over a decade, and I can't remember the last time I even came close to needing to use them.


I think you should really consider using references instead of $$, like this:

$arr = array();

$path = &$arr['item']['abc'];

$path[] = array(
       'name'=>'somename',
       'other'=>'...'
);

print_r($path);

EDIT: Another option based on the new details coming from the author:

Lets consider the case when indexes are stored in a separate array, 'dynamically' as you call it. 'item' index is always the same - let's just omit it.

$arr = array();

//Throwing out the 'item' index, leave only the important keys here
$loc_arr = array("abc","www","ccc");

foreach($loc_arr as $item){
    $arr['item'][$item] = array(
       'name'=>'somename',
       'other'=>'...'
    );
}

print_r($arr);

And even if the 'item' element wouldn't have the same name all the time - you could use 2 dimensions to specify the right one. Add some recursive function and it will iterate over the thing and set values accordingly. Just another option for you.

$loc_arr = array('item' => array('abc','www','ccc'), 'foo' => array('bar', 'bar2'));


 MORE:


 ? Get select option value from forms using jquery created dynamically in loop
 ? Get select option value from forms using jquery created dynamically in loop
 ? Get select option value from forms using jquery created dynamically in loop
 ? How do I create a dynamic form in Drupal 6 using jQuery?
 ? How do I get the data from forms dynamically created with jQuery into php?
 ? Dynamically selecting Dropdown's options
 ? jquery trigger button click, and get value of dynamic select/option
 ? Can't get value of select field in a form with jquery
 ? How to set readonly property to false on multiple textboxes with same class?
 ? How to set readonly property to false on multiple textboxes with same class?