php how to generate dynamic list()?

2019-02-25 18:36发布

问题:

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 value out from array accordingly.
here is my question... how to generate a dynamic list()?

1). base on database return result, I'm not sure how many of it but I assign it all into array
2). so we can use count(array) to know how many of it.
3). so then HOW CAN I GENERATE/PREPARE a list for it?

Example: client A, have 3 kids, name Apple, Boy, Cat so I use list($kid1, $kid2, $kid3) for it.

but when client B, have more then 3 kids, I only get first 3 or if client C, have only 1 kids, then error encounter.

I know if base on the situation above, there is many way to solve it without using list() but I wish to know or find out the solution with using list().

How to generate dynamic list() base on count of array()

thanks guys/gals

回答1:

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;
}


回答2:

You can create a lambda expression with create_function() for this. The list() will be only accessible within the expression.



回答3:

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 ;)



回答4:

This is not what PHP's list is meant for. From the official PHP docs

list is not really a function, but a language construct. 
list() is used to assign a list of variables in one operation.

In other words, the compiler does not actually invoke a function but directly compiles your code into allocations for variables and assignment.

  • You can specifically skip to a given element, by setting commas as follows:

    list($var1, , $var2) = Array($B1, B2, B3);

    echo "$var1 is before $var2 \n";

  • or take the third element

    list( , , $var3) = Array($B1, B2, B3);

(I am assuming B2, B3 are constants? Or are you missing a $?)

Specifically using list, you can use PHP's variable variables to create variables from an arbitrary one-dimensional array as follows:

$arr = array("arrindex0" => "apple", "banana", "pear");
reset($arr);
while (list($key, $val) = each($arr)) {
    $key = is_numeric($key) ? "someprefix_" . $key : $key;
    echo "key sdf: $key <br />\n";
    $$key = $val;
}
var_dump($arrindex0, $someprefix_0, $someprefix_1);

Result

string 'apple' (length=5)
string 'banana' (length=6)
string 'pear' (length=4)