I have an array like this:
$data = array(
"ID" => 1,
"NAME" => "John Doe",
"DATE" => date("d.m.Y H:i:s")
);
I want to create new variables with the name of the key and the value of the key like this:
$id = 1;
$name = "John Doe";
$date = "17.11.2016 00:00:00";
I would like to do this with in for each loop, my current code looks like this:
foreach ($data as $key => $value) {
$key = $data[$key];
}
PHP supports variable variables, although this is poor design you can just do:
foreach ($data as $key => $value) {
$$key = $data[$key];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With