0

What I am doing that I want to generate a list based on how many items are in an array, so I have counted the items and loop over them, create a number based var and construct a string $var which contains $a1,$a2.... and assigns the $var to list list($var)

and tried to access $a1 but it gives me the error "Undefined variable: a1"

Is there any other way to do it?

Here is my code:

$arr = array('1','2','3');
$listsize = count($arr);
$var='';
for($i=1;$i<=$listsize;$i++){
        $var.='$a'.$i;
        if($i!=$listsize){
            $var.=',';
        }
}

list($var) = $arr;
echo $a1;
3
  • 2
    Exact duplicate of php how to generate dynamic list()? Commented Sep 23, 2012 at 8:24
  • where is $a defined? you have just assigned its value to $var like this $var.='$a'.$i; Commented Sep 23, 2012 at 8:24
  • $a is just newly created var i am using that as string and producing the $a1 for use in list by above code echo $var will output as $a1,$a2,$a3 and using list($var) is my means by list($a1,$a2,$a3) = $arr and echo $a1 should be 1 is it clear Commented Sep 23, 2012 at 8:33

3 Answers 3

1

What you are looking for is variable variables.

In PHP, you can dynamically assign variables names (not just values).

Here is an example:

$foo = "Hello" . 1;

# In this line, I am taking the value of the variable $foo (Hello1) and
# using that as as a variable name. This is equivalent to
# $Hello1 = "World", except the variable is dynamic (hence variable variables).

$$foo = "World";

print $Hello1; # This will print World
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Burhan this seems a considerable answer could you please include a example of using variable variables in this case.
Burhan - as per @ravisoni's comment it would be nice if you could expand your answer to provide a little more information.
Actually the question is an exact duplicate; which is why I didn't expand on my answer. I will do so now.
1

Why not use extract()?

Try this:

$values = array('1','2','3');
$variables = array();
$length = count($values);
$key = 'a1';

for ($i = 0; $i < $length; $i++){
  $variables[$key] = $values[$i];
  $key++;
}

extract($variables);
echo $a1, $a2, $a3;

Comments

0

You can solve your problem without loops. Array $as is filled with your data, that has keys a1 to aX:

$arr = array('1', '2', '3', 'test', true, 4.56);
$keys = array_map(function($n) { return "a$n"; }, range(1, count($arr)) );
$a = array_combine($keys, $arr);

Array $as has keys and values like output bellow:

Array
(
    [a1] => 1
    [a2] => 2
    [a3] => 3
    [a4] => test
    [a5] => 1
    [a6] => 4.56
)

I advice you to use access to variables via array like $a['a3'], and not via variables like $a3.

If you would like to have $a1 ... $aX variables, extract array like:

extract($a);

Comments

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.