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
http://codepad.viper-7.com/Dc8Jei
Updated -> http://codepad.viper-7.com/k4sIgJ
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With