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
Post a Comment