0

I tried the entire afternoon, but just cannot figure how to get this done.

<?php
$e['ea'] = '01';
$e['eb'] = '02';
$e['ec'] = '03';

$a = array(
    'e' => $e,
    'h'=>'ec',
    'p'=>[
        'in' => [
            ['in' => '01'],
            ['in' => '02'],
            ['in' => '03']
        ]
    ]
);

$i['ia'] = '04';
$i['ib'] = '04';
$i['ic'] = '04';

$path = '$a[\'e\'][\'ed\']';

${'path'} = $i; //Here is where I'm stuck

echo '<pre>';
print_r($a);
echo '</pre>';
?>

I have the path to a key stored as $path, that I'm trying to use to assign the value of $i.

But it somehow won't work. How can I achieve this?

Norman
  • 6,159
  • 23
  • 88
  • 141
  • 1
    Unless you use `eval()`, then this isn't going to work exactly as you want, you could get it to work with extra decoding and an alternative style (something like `'a,e,ed'` could work). – Nigel Ren Jun 03 '20 at 14:25
  • Related: https://stackoverflow.com/questions/27929875/how-to-write-getter-setter-to-access-multi-level-array-by-key-names – Barmar Jun 03 '20 at 14:40
  • Would `eval` be `evil` if used here? – Norman Jun 03 '20 at 14:41
  • can you elaborate on what your end goal is, maybe there is a better way to handle this? – YouriKoeman Jun 03 '20 at 14:46
  • @YouriKoeman I'm trying to do `$path = '$a[\'e\'][\'ed\']';` so I can use it as `$path="value for this key` so I can avoid using the entire path when setting the value. – Norman Jun 03 '20 at 14:49
  • Have you looked into helper classes that some framworks provide? like: https://book.cakephp.org/3/en/core-libraries/hash.html or: https://www.yiiframework.com/doc/guide/2.0/en/helper-array You dont need the framework for this you can just import the class only – YouriKoeman Jun 03 '20 at 15:11

0 Answers0