Take the 2-minute tour ×

Which method is good to get values from array using in loops.

 1. list()
 2. extract()
 3. Normal extracting like $array['id']

code snippet:

$sel = mysql_query("SELECT id,name from user");
while($get = mysql_fetch_array($sel)){
 //Which one is good
 $id = $get['id'];

I like to know pros and cons of these methods. or any other method exists.


share|improve this question
Use the tool appropriate for the specific job. –  alex Jun 6 '11 at 4:33
add comment

2 Answers

up vote 4 down vote accepted

extract() should be used with most care. But it is perfectly fine if you want to get a limited set of known variables from an associative array.

list() however is usually unsuited for associative arrays. It's for numerically indexed arrays, and assigns them to local variables. list() is sort of equivalent to extract(array_combine($list_keys, $array_values));

Finally the normal array access, most common and most advisable if you only need to access a few array entries. Very obviously also preferred most everywhere because it leads to the least conflicts with existing local variables.

share|improve this answer
Setting the extract_type parameter will give you a lot of ways around conflicting variable names. –  Wesley Murch Jun 6 '11 at 4:55
add comment

Avoid extract() wherever possible (which is most places). It does things to the symbol table that sane languages do not allow.

Between list() and explicit array extraction, list() is probably preferable, as it's a bit more concise. It requires that you have an ordered array, though, rather than an associative array.

share|improve this answer
add comment

Your Answer


