php - Using a string path to set nested array data -


i have unusual use-case i'm trying code for. goal this: want customer able provide string, such as:

"cars.honda.civic = on" 

using string, code set value follows:

$data['cars']['honda']['civic'] = 'on'; 

it's easy enough tokenize customer input such:

$token = explode("=",$input); $value = trim($token[1]); $path = trim($token[0]); $exploded_path = explode(".",$path); 

but now, how use $exploded path set array without doing nasty eval?

use reference operator successive existing arrays:

$temp = &$data; foreach($exploded $key) {     $temp = &$temp[$key]; } $temp = $value; unset($temp); 

Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -