How to assign variable dynamically to php list function
        Posted  
        
            by 
                ravisoni
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by ravisoni
        
        
        
        Published on 2012-09-23T08:21:09Z
        Indexed on 
            2012/09/23
            9:37 UTC
        
        
        Read the original article
        Hit count: 184
        
php
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;
© Stack Overflow or respective owner