How to assign variable dynamically to php list function
- by ravisoni
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;