PHP: list()
Posted by: Jordan in Programming, PHP on Jul 06, 2008
I've never seen the list function used with arrays, that I can remember, when modifying PHP code written by someone else. The language construct seems like a very useful tool when iterating through an array and I couldn't help but benchmarking it.
The list construct assigns variables to array values without having to manually specify each one. Basically, it automates the process of variable assignment for arrays which comes in handy while looping.
Simple example of list:

which will display "coffee is brown and caffeine makes it special". What perked my interest was assigning variable names to mysql database results:

Using each() you can use list() with while and for:


It is important that you remember to reset the array pointer to the beginning of the array. You can do this with the reset() function.
Benchmarking
I decided to do benchmarking using the Pear module described here: PHP PEAR Benchmarking. I tested the list construct against the common array iteration methods for, foreach and while:
You can see that using while with list is faster than all other methods tested here while using for with list is by far the slowest method. In case anyone would like to replicate my test here is the PHP script (you'll also need the PEAR Benchmarking installed).

Need Help?
Ask on our forum!
The list construct assigns variables to array values without having to manually specify each one. Basically, it automates the process of variable assignment for arrays which comes in handy while looping.
Simple example of list:

- $info = array('coffee', 'brown', 'caffeine');
- // Listing all the variables
- list($drink, $color, $power) = $info;
- echo "$drink is $color and $power makes it special.n";
$info = array('coffee', 'brown', 'caffeine');
// Listing all the variables
list($drink, $color, $power) = $info;
echo "$drink is $color and $power makes it special.n";
// Listing all the variables
list($drink, $color, $power) = $info;
echo "$drink is $color and $power makes it special.n";
which will display "coffee is brown and caffeine makes it special". What perked my interest was assigning variable names to mysql database results:

- $result = mysql_query("SELECT id, name, salary FROM employees", $conn);
- while (list($id, $name, $salary) = mysql_fetch_row($result)) {
- ....
$result = mysql_query("SELECT id, name, salary FROM employees", $conn);
while (list($id, $name, $salary) = mysql_fetch_row($result)) {
....
while (list($id, $name, $salary) = mysql_fetch_row($result)) {
....
Using each() you can use list() with while and for:

- $myArray = array ( 'a'=>'v1',
- 'b'=>'v2',
- 'c'=>'v3'
- );
- reset($array);
- while (list($key, $value) = each($myArray)) {
- print "$key => $value" . PHP_EOL;
- }
$myArray = array ( 'a'=>'v1',
'b'=>'v2',
'c'=>'v3'
);
reset($array);
while (list($key, $value) = each($myArray)) {
print "$key => $value" . PHP_EOL;
}
'b'=>'v2',
'c'=>'v3'
);
reset($array);
while (list($key, $value) = each($myArray)) {
print "$key => $value" . PHP_EOL;
}

- $myArray = array ( 'a'=>'v1',
- 'b'=>'v2',
- 'c'=>'v3'
- );
- for (reset($array); list($key, $value) = each($array);) {
- print "$key => $value" . PHP_EOL;
- }
$myArray = array ( 'a'=>'v1',
'b'=>'v2',
'c'=>'v3'
);
for (reset($array); list($key, $value) = each($array);) {
print "$key => $value" . PHP_EOL;
}
'b'=>'v2',
'c'=>'v3'
);
for (reset($array); list($key, $value) = each($array);) {
print "$key => $value" . PHP_EOL;
}
It is important that you remember to reset the array pointer to the beginning of the array. You can do this with the reset() function.
Benchmarking
I decided to do benchmarking using the Pear module described here: PHP PEAR Benchmarking. I tested the list construct against the common array iteration methods for, foreach and while:
| | time index | ex time | % |
| Start | 1215293315.27111400 | - | 0.00% |
| List | 1215293315.27191700 | 0.000803 | 5.52% |
| Foreach | 1215293315.27408900 | 0.002172 | 14.93% |
| While | 1215293315.27623400 | 0.002145 | 14.74% |
| ForList | 1215293315.28323200 | 0.006998 | 48.09% |
| For | 1215293315.28562400 | 0.002392 | 16.44% |
| Stop | 1215293315.28566600 | 0.000042 | 0.29% |
| total | - | 0.014552 | 100.00% |
You can see that using while with list is faster than all other methods tested here while using for with list is by far the slowest method. In case anyone would like to replicate my test here is the PHP script (you'll also need the PEAR Benchmarking installed).

- require 'Benchmark/Timer.php';
- // Create a new array
- $newArray = array ( 'a'=>'v1',
- 'b'=>'v2',
- 'c'=>'v3'
- );
- // Pass the param "true" to constructor to automatically display the results
- $timer = new Benchmark_Timer();
- $timer->start();
- // Start with list
- loop_list($newArray);
- $timer->setMarker('List');
- $timer->timeElapsed('Start', 'List') . "n";
- // Do foreach
- loop_foreach($newArray);
- $timer->setMarker('Foreach');
- $timer->timeElapsed('List', 'Foreach') . "n";
- // Do foreach
- loop_foreach($newArray);
- $timer->setMarker('While');
- $timer->timeElapsed('ForEach', 'While') . "n";
- // Do ForList
- loop_forlist($newArray);
- $timer->setMarker('ForList');
- $timer->timeElapsed('While', 'ForList') . "n";
- // Do For
- loop_for($newArray);
- $timer->setMarker('For');
- $timer->timeElapsed('ForList', 'For') . "n";
- // Stop the microtimer and
- // Display the Results
- $timer->stop();
- $timer->display();
- /**
- * Loop through an array using while list
- *
- * @param unknown_type $array
- */
- function loop_list($array) {
- reset($array);
- for($i=0;$i<1000;$i++) {
- while (list($key, $value) = each($array)) {
- }
- }
- }
- /**
- * Loop through an array using foreach
- *
- * @param unknown_type $array
- */
- function loop_foreach($array) {
- for($i=0;$i<1000;$i++) {
- foreach($array as $key => $var) {
- }
- }
- }
- /**
- * Loop through an array using while
- *
- * @param unknown_type $array
- */
- function loop_while($array) {
- for($i=0;$i<1000;$i++) {
- $p=0;
- while ($p < count($array)) {
- $p++;
- }
- }
- }
- /**
- * Loop through array using "for"
- * with list
- *
- * @param unknown_type $array
- */
- function loop_forlist($array) {
- for($i=0;$i<1000;$i++) {
- for (reset($array); list($key, $value) = each($array);) {
- }
- }
- }
- /**
- * Loop through array using "for"
- * without list
- *
- * @param unknown_type $array
- */
- function loop_for($array) {
- for($i=0;$i<1000;$i++) {
- for ($p=0; $p<count($array); $p++)="" {="">
- }
- }
- }
require 'Benchmark/Timer.php';
// Create a new array
$newArray = array ( 'a'=>'v1',
'b'=>'v2',
'c'=>'v3'
);
// Pass the param "true" to constructor to automatically display the results
$timer = new Benchmark_Timer();
$timer->start();
// Start with list
loop_list($newArray);
$timer->setMarker('List');
$timer->timeElapsed('Start', 'List') . "n";
// Do foreach
loop_foreach($newArray);
$timer->setMarker('Foreach');
$timer->timeElapsed('List', 'Foreach') . "n";
// Do foreach
loop_foreach($newArray);
$timer->setMarker('While');
$timer->timeElapsed('ForEach', 'While') . "n";
// Do ForList
loop_forlist($newArray);
$timer->setMarker('ForList');
$timer->timeElapsed('While', 'ForList') . "n";
// Do For
loop_for($newArray);
$timer->setMarker('For');
$timer->timeElapsed('ForList', 'For') . "n";
// Stop the microtimer and
// Display the Results
$timer->stop();
$timer->display();
/**
* Loop through an array using while list
*
* @param unknown_type $array
*/
function loop_list($array) {
reset($array);
for($i=0;$i<1000;$i++) {
while (list($key, $value) = each($array)) {
}
}
}
/**
* Loop through an array using foreach
*
* @param unknown_type $array
*/
function loop_foreach($array) {
for($i=0;$i<1000;$i++) {
foreach($array as $key => $var) {
}
}
}
/**
* Loop through an array using while
*
* @param unknown_type $array
*/
function loop_while($array) {
for($i=0;$i<1000;$i++) {
$p=0;
while ($p < count($array)) {
$p++;
}
}
}
/**
* Loop through array using "for"
* with list
*
* @param unknown_type $array
*/
function loop_forlist($array) {
for($i=0;$i<1000;$i++) {
for (reset($array); list($key, $value) = each($array);) {
}
}
}
/**
* Loop through array using "for"
* without list
*
* @param unknown_type $array
*/
function loop_for($array) {
for($i=0;$i<1000;$i++) {
for ($p=0; $p<count($array); $p++)="" {="">
}
}
}
// Create a new array
$newArray = array ( 'a'=>'v1',
'b'=>'v2',
'c'=>'v3'
);
// Pass the param "true" to constructor to automatically display the results
$timer = new Benchmark_Timer();
$timer->start();
// Start with list
loop_list($newArray);
$timer->setMarker('List');
$timer->timeElapsed('Start', 'List') . "n";
// Do foreach
loop_foreach($newArray);
$timer->setMarker('Foreach');
$timer->timeElapsed('List', 'Foreach') . "n";
// Do foreach
loop_foreach($newArray);
$timer->setMarker('While');
$timer->timeElapsed('ForEach', 'While') . "n";
// Do ForList
loop_forlist($newArray);
$timer->setMarker('ForList');
$timer->timeElapsed('While', 'ForList') . "n";
// Do For
loop_for($newArray);
$timer->setMarker('For');
$timer->timeElapsed('ForList', 'For') . "n";
// Stop the microtimer and
// Display the Results
$timer->stop();
$timer->display();
/**
* Loop through an array using while list
*
* @param unknown_type $array
*/
function loop_list($array) {
reset($array);
for($i=0;$i<1000;$i++) {
while (list($key, $value) = each($array)) {
}
}
}
/**
* Loop through an array using foreach
*
* @param unknown_type $array
*/
function loop_foreach($array) {
for($i=0;$i<1000;$i++) {
foreach($array as $key => $var) {
}
}
}
/**
* Loop through an array using while
*
* @param unknown_type $array
*/
function loop_while($array) {
for($i=0;$i<1000;$i++) {
$p=0;
while ($p < count($array)) {
$p++;
}
}
}
/**
* Loop through array using "for"
* with list
*
* @param unknown_type $array
*/
function loop_forlist($array) {
for($i=0;$i<1000;$i++) {
for (reset($array); list($key, $value) = each($array);) {
}
}
}
/**
* Loop through array using "for"
* without list
*
* @param unknown_type $array
*/
function loop_for($array) {
for($i=0;$i<1000;$i++) {
for ($p=0; $p<count($array); $p++)="" {="">
}
}
}
Need Help?
Ask on our forum!
