0

I'm trying to pass an array of variable names to be assigned to the list() function, and I'm wondering if it's even possible. My intention is for list($variables) to parse $values.

$variables = array("$var1","$var2","$var3");
$values = array('Value1','Value2','Value3');

//Can I simply pass an array of variable names to be assigned here
list($values) = explode("&", $values);

To clarify, my intention is to have PHP execute this:

list($var1, $var2, $var3) = explode("&", $values);
2
  • what error you have or what part of code is not working Commented Jul 15, 2014 at 16:16
  • I simply want to execute the last line of code, by passing an array of the the variable names to list(). There is no part of the code that is working. I don't think it's possible to do. Commented Jul 15, 2014 at 16:24

5 Answers 5

2

You'd have to do a little trickery, but with array_combine and extract you could achieve the same effect:

$keys = array("var1","var2","var3");
$values = array('Value1','Value2','Value3');
extract(array_combine($keys, $values));
echo $var1; //"Value1"
Sign up to request clarification or add additional context in comments.

2 Comments

You are literally the only person who understood the question. Thank you for your help, much appreciated.
you could also use something like call_user_func_array, but i think this way is more understandable honestly EDIT: Here's the man page for that php.net/manual/en/function.call-user-func-array.php
1
<?php

$info = array('coffee', 'brown', 'caffeine');

// Listing all the variables
list($drink, $color, $power) = $info;
echo "$drink is $color and $power makes it special.\n";

// Listing some of them
list($drink, , $power) = $info;
echo "$drink has $power.\n";

// Or let's skip to only the third one
list( , , $power) = $info;
echo "I need $power!\n";

// list() doesn't work with strings
list($bar) = "abcde";
var_dump($bar); // NULL
?>

http://php.net/manual/en/function.list.php

1 Comment

This is exactly what I am looking for. It was widely used to extract values from MySQL database.
0

list is used to assign a list of variables in one operation. This is the sytax:

$string = 'VarA, VarB, VarC';
list($varA, $varB, $varC) = explode(',', $string);

When u use variables you can need to keep the quotes away so the second is the right one.

$array = ($var1, $var2, $var3);

You dont need to use explode anymore cause you already have an array. explode creates an array. In your example it would be:

$array = array($var1, $var2, $var3);
list($varA, $varB, $varC) = $array;

Note that i changed the array syntax. You forgot the array keyword.

3 Comments

My intention is to pass an array of the variable names to be assigned.
If you want to pass an Array of variable names you can use $array = array('var1', 'var2', 'var3'); list($varA, $varB, $varC) = $array; but i don't see the purpose of this..
Say you have 50 variable names you want to assign from an API response, declaring 50 variable names in the list() function does not seem efficient. Much easier to use arrays that you can pass to a function on call.
0

I understand you want to explode the response variable and assign each value to the corresponding variable.

You can achieve it this way instead of using list:

$varnames = array('var1','var2','var3');
$varvals  = explode('&',$response);
foreach($varnames as $k => $v){
    $$v = $varvals[$k];
}

This way $var1 will have the first value in response, $var2 the second, and so on.

Comments

-1

You can't do that. Why you don't use the second your variant?

2 Comments

I didn't downvote but they did because the second variant list($var1, $var2, $var3) = explode("&", $response); is also incorrect.
Okay. I just have said about the «list()» struct syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.