Array path from variable in PHP

后端 未结 3 1123
时光说笑
时光说笑 2021-01-27 12:51

So I wrote a class that can parse XML documents and create SQL queries from it to update or insert new rows depending on the settings.

Because the script has to work wit

3条回答
  •  时光取名叫无心
    2021-01-27 13:12

    You can use a foreach with variable variables.

    // assuming a base called $array, and the path as in your example:
    $path = array('field1','field2');
    
    $$path = $array;
    foreach ($path as $v) $$path = $$path[$v];
    $$path['value'] = 'test';
    

    Short, simple, and much better than eval.

提交回复
热议问题