0

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

1
  • 1
    This idea started at bad and flew at top speed straight into terrible. Why do you think you want this? Commented Jul 17, 2012 at 5:38

4 Answers 4

4

How about

$choices = explode(',', $_POST['choices']);
foreach ($choices as $choice){
    $$choice = $choice;
}
Sign up to request clarification or add additional context in comments.

3 Comments

That's correct code for the solution, but it sends shivers down my spine. Only bad things can happen in PHP using $$.
@Fluffeh Can you elaborate please? I'm only asking because I use variable variables a lot and would like to know what could be the consequences of over using them. Thanks
Most of my code is user-input driven, which means a lot of poor data entry. The last thing I want to do is start naming my variables based on user-input. What if their input starts with a number, or contains a * in it? Not saying there isn't a value in variable variables, it's a cool feature, it really is, but I have never been game to use it in anything other than a test. I have never wanted to go vetting the input to that level of cleanliness. UserName: bob7#-3po I can live with, PHP Trying to turn that into a variable, I can't.
1
$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.

Comments

1

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.

Comments

0

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;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.