2

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

7
  • 3
    Why would you want to use list? Why not simply access them by their index? There seems to be no gain by using list in this case... Commented Sep 23, 2012 at 6:12
  • 1
    @JosephSilber 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(). from question Commented Sep 23, 2012 at 6:13
  • Joseph, he did state why: curiosity Commented Sep 23, 2012 at 6:13
  • 2
    You can use extract() on the array, to create variables inside the current scope... Commented Sep 23, 2012 at 6:16
  • @LoSauer - That only works on an associative array. Commented Sep 23, 2012 at 6:17

4 Answers 4

5

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for That's insane use of variables. And of course it's a simple and readable solution.
0

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

Comments

0

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

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.