MarkP
MarkP

Reputation: 2566

PHP get array() value to become $variable

ok, So I have this array:

 $choices = array($_POST['choices']);

and this outputs, when using var_dump():

array(1) { [0]=> string(5) "apple,pear,banana" }

What I need is the value of those to become variables as well as adding in value as the string. so, I need the output to be:

 $apple = "apple";
 $pear = "pear";
 $banana = "banana";

The value of the array could change so the variables have to be created depending on what is in that array.

I would appreciate all help. Cheers

Mark

Upvotes: 0

Views: 1857

Answers (4)

Lachlan Pease
Lachlan Pease

Reputation: 728

I have to concur with all the other answers that this is a very bad idea, but each of the existing answers uses a somewhat roundabout method to achieve it.

PHP provides a function, extract, to extract variables from an array into the current scope. You can use that in this case like so (using explode and array_combine to turn your input into an associative array first):

$choices = $_POST['choices'] ?: ""; // The ?: "" makes this safe even if there's no input
$choiceArr = explode(',', $choices); // Break the string down to a simple array
$choiceAssoc = array_combine($choiceArr, $choiceArr); // Then convert that to an associative array, with the keys being the same as the values
extract($choiceAssoc, EXTR_SKIP); // Extract the variables to the current scope - using EXTR_SKIP tells the function *not* to overwrite any variables that already exist, as a security measure
echo $banana; // You now have direct access to those variables

For more information on why this is a bad approach to take, see the discussion on the now deprecated register_globals setting. In short though, it makes it much, much easier to write insecure code.

Upvotes: 1

House.Lee
House.Lee

Reputation: 251

$str = "apple,pear,pineapple";
$strArr = explode(',' , $str);
foreach ($strArr as $val) {
    $$val = $val;
}
var_dump($apple);

This would satisfy your requirement. However, here comes the problem, since you could not predefine how many variables are there and what are they, it's hard for you to use them correctly. Test "isset($VAR)" before using $VAR seems to be the only safe way.

You'd better just split the source string in just one array and just operate the elements of the specific array.

Upvotes: 1

Musa
Musa

Reputation: 97672

How about

$choices = explode(',', $_POST['choices']);
foreach ($choices as $choice){
    $$choice = $choice;
}

Upvotes: 4

eddieantonio
eddieantonio

Reputation: 170

Often called "split" in other langauges, in PHP, you'd want to use explode.

EDIT: ACTUALLY, what you want to do sounds... dangerous. It's possible (and was an old "feature" of PHP) but it's strongly discourage. I'd suggest just exploding them and making their values the keys of an associative array instead:

$choices_assoc = explode(',', $_POST['choices']);
foreach ($choices as $choice) {
    $choices_assoc[$choice] = $choice;
}

Upvotes: 0

Related Questions