Take the 2-minute tour ×

I have an array:

$arr = array('foo', 'bar', 'bash', 'monkey', 'badger');

I want to have the elements in that array appear as the variables in my list():

list($foo, $bar, $bash, $monkey, $badger) = $data;

Without actually specifying the variables, I tried;

list(implode(",$", $arr)) = $data; and
list(extract($arr)) = $data;

But they don't work, I get:

Fatal error: Can't use function return value in write context

Does anyone have any idea whether this is possible?

UPDATE: more context:

I am getting a CSV of data from an API, the first row is column names, each subsequent row is data. I want to build an associative array that looks like this:

$data[0]['colname1'] = 'col1data';
$data[0]['colname2'] = 'col2data';
$data[0]['colname3'] = 'col3data';
$data[1]['colname1'] = 'col1data';
$data[1]['colname2'] = 'col2data';
$data[1]['colname3'] = 'col3data';

Before I do that, however, I want to make sure I have all the columns I need. So, I build an array with the column names I require, run some checks to ensure the CSV does contain all the columns I need. Once thats done, the code looks somewhat like this (which is executed on a foreach() for each row of data in the CSV):

//$data is an array of data WITHOUT string indexes
list(   $col1,
 ) = $data;

foreach($colNames AS $name)
    $newData[$i][$name] = $$name;
// Increemnt

As I already HAVE an array of column name, I though it would save some time to use THAT in the list function, instead of explicitly putting in each variable name.

The data is cleaned and sanitised elsewhere.



share|improve this question
Most of time such a question is a sign of bad design. Why do you need dynamically assigned variable names? –  Your Common Sense Apr 20 '10 at 12:22
Wild guess... Are you trying to emulate register_globals? –  Álvaro G. Vicario Apr 20 '10 at 12:28
what is your need ? Please explain, because answer are posted by different ways of understanding this issue. –  Karthik Apr 20 '10 at 12:29
Col. Shrapnel - How can you make that statement without more context? Maybe just help me out instead of assuming I'm ignorant. Alvaro - No. Karthik - See edited question. –  Mike Apr 20 '10 at 13:05
how do you plan on using the dynamically created variable names later on in your code? i think you are overcomplicating things. simply use $col1…$col10 and everything should work –  knittl Apr 20 '10 at 13:58

6 Answers 6

up vote 1 down vote accepted

You are trying to use a language construct in a manner in which it's not meant to be used. Use variable variables as Alvaro mentioned.

foreach ($arr as $index => $key) {
    $$key = $data[$index];


$result = array();
    $result[$key] = $data[$index];

In short, do not use "list", use arrays and associated arrays. Write helper functions to make your code clearer.

share|improve this answer
Perfect! I knew I was missing something. Seems I forgot that foreach($arr as $index.... would return the integer indices. Thanks razzed, thanks Alvaro. –  Mike Apr 20 '10 at 15:25


i think there is your problem in understanding. list() does not create a new list structure or variable, it can only be used to assign many variables at once:

$arr = array(1, 2, 3);
list($first, $second, $third) = $arr;
// $first = 1, $second = 2, $third = 3

see http://php.net/list for more information.

you are probably looking for an associative array. you can create it with the following code:

$arr = array('first' => 1, 'second' => 2, 'third' => 3);
// $arr['first'] = 1, …
share|improve this answer
I understand list(). It DOES create a new variables from indexes in an array. I do want an associative array, but I want the string indices to be set from a separate array. –  Mike Apr 20 '10 at 13:25
@mike, you can also do @array[@string_index] if that's what you are looking for –  knittl Apr 20 '10 at 15:17

If some rows in your input file are missing columns, you can't really know which one is missing. Counting the number of values and aborting or jumping to next row when less than expected should be enough.

... unless you set the rule that last columns are optional. I'll elaborate on this.

Your code sample is far for complete but it seems that your problem is that you are using arrays everywhere except when matching column names to cell values. Use arrays as well: you don't need individual variables and they only make it harder. This is one of the possible approaches, not necessarily the best one but (I hope) clear enough:


$required_columns = array('name', 'age', 'height');
$input_data = array(
    array('John', 33, 169),
    array('Bill', 40, 180),
    array('Ashley', 21, 155),
    array('Vincent', 13), // Incomplete record
    array('Michael', 55, 182),
$output = array();

foreach($input_data as $row_index => $row){
    foreach($required_columns as $col_index => $column_name){
        if( isset($row[$col_index]) ){
            $output[$row_index][$column_name] = $row[$col_index];



I've used $row_index and $col_index for simplicity.

Original answer, for historical purposes only ;-)

I can't really understand your specs but PHP features variable variables:



foreach($arr as $i){
    $$i = $i;


Now your script has these variables available: $foo, $bar... It's quite useless and potentially dangerous but it does what you seem to need.

share|improve this answer
Alvaro, using list($v1, $v2) = $data, where $data is an array, passes each of the indexes of the array into the variables. Your script above just creates vars with the varname as the value. –  Mike Apr 20 '10 at 13:23
@Mike: That's what I figured out the original example was trying to do: list($foo, $bar, $bash, $monkey, $badger) = $data; –  Álvaro G. Vicario Apr 20 '10 at 14:10

why not immediatly call the vars like



share|improve this answer
I want to avoid using $arr[0] for clarity. –  Mike Apr 20 '10 at 13:24

If you want to extract the elements of $data into the current context, you can do that with extract. You might find it useful to call array_intersect_key first to pare down $data to the elements that you want to extract.

share|improve this answer

May be try like this :

$data = "$".implode(",", $arr);
echo str_replace(",", ",$", $data);
share|improve this answer

Your Answer


