If I have an array
$places = array('year' => '2012', 'place' => 'school');
Is there any way of doing this in PHP
foreach ($places as $key => $value)
{
$key = $value
}
But so that variables are being set based on the name of the key.
Eg, variables would be available like so
echo $year;
2012
echo $place;
school
Use extract
extract($places)
echo $year;
echo $place;
Or, you could use variable-variables:
foreach ($places as $key => $value)
{
$$key = $value //note the $$
}
Why can't you just do like this ?
<?php
$places = array('year' => '2012', 'place' => 'school');
echo $places['year'];// prints 2012 This is also synonymous to a variable
AFAIK, you are just complicating your example.
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