Jump to content

[SOLVED] Array to Variables


bdmovies

Recommended Posts

$info is an array:

 

<?php 
foreach($info as $key => $value) {
   $key = $value;
   echo "$key = ".$value . "<br/>";
}
?>

 

This prints VALUE = VALUE, not KEY = VALUE.

 

Why wouldn't the key get stored as the variable name?

 

This particular array comes from a form with nearly several inputs, so I wanted to streamline the variable naming process by using the loop, what am I doing wrong?

Link to comment
https://forums.phpfreaks.com/topic/167325-solved-array-to-variables/
Share on other sites

that's because you're just assigning the value of $value to the variable $key. if you want to use $key as the variable name, you need to use variable variable notation:

 

$$key = $value;

 

however, the extract() function will do this for you.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.