Using a String Path to Set Nested Array Data
Question:
How can I dynamically set nested array data using a string path, such as "cars.honda.civic" to $data'cars'['civic'] without relying on eval()?
Answer:
The reference operator (&) allows for this dynamic setting:
$temp = &$data; foreach ($exploded_path as $key) { $temp = &$temp[$key]; } $temp = $value; unset($temp);
By using this approach, you can efficiently set the nested array data without the need for eval(). Here's how it works:
The above is the detailed content of How to Dynamically Set Nested Array Data Using a String Path in PHP?. For more information, please follow other related articles on the PHP Chinese website!