You want to extract the variables from an array only if the array is created with string as keys.
Use extract(). It creates variables corresponding to te string indices.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN"> <head> <title>PHP</title> <meta Name="Author" Content="Hann So"> </head> <body> <p> <?php $fruits = array('red'=>'Apples', 'green'=>'Grapes', 'yellow'=>'Bananas', 'orange'=>'Oranges'); print_r($fruits); extract($fruits); echo "<p><b>Extract from an asociated array.</b></p>"; echo "\$red = $red<br />"; echo "\$green = $green<br />"; echo "\$yellow = $yellow<br />"; echo "\$orange = $orange<br />"; ?> </p> </body> </html> |