php how to generate dynamic list()?

前端 未结 4 1402

base on my understanding, this how list() work.

list($A1,$A2,$A3) = array($B1,B2,B3);

So with the help of list() we can assign val

4条回答
  •  北恋
    北恋 (楼主)
    2021-01-22 00:02

    This creates variables $A1, $A2, .... $AN for each element in your array:

    $list =  array("a", "b", "c", "d");
    
    extract(array_combine(array_map(function($i) {
            return "A" . $i;
        }, range(1, count($list))), $list));
    
    echo implode(" ", array($A1, $A2, $A3, $A4)), PHP_EOL;
    

    You can modify the name of the variables in the array_map callback. I hope I'll never see code like that in production ;)

提交回复
热议问题