phparraysvariables

Extract associative array elements into individual variables


How do I go about setting a string as a literal variable in PHP? Basically I have an array like

$data['setting'] = "thevalue";

and I want to convert that 'setting' to $setting so that $setting becomes "thevalue".


Solution

  • See PHP variable variables.

    Your question isn't completely clear but maybe you want something like this:

    //Takes an associative array and creates variables named after
    //its keys
    foreach ($data as $key => $value) {
        $$key = $value;
    }