php how to generate dynamic list()?

前端 未结 4 1403

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:00

    If you have a variable number of elements, use arrays for them! It does not make sense to extract them into individual variables if you do not know how many variables you'll be dealing with. Say you did extract those values into variables $kid1 through $kidN, what is the code following this going to do? You have no idea how many variables there are in the scope now, and you have no practical method of finding out or iterating them next to testing whether $kid1 through $kidN are isset or not. That's insane use of variables. Just use arrays.

    Having said that, variable variables:

    $i = 1;
    foreach ($array as $value) {
        $varname = 'kid' . $i++;
        $$varname = $value;
    }
    

提交回复
热议问题