PHP: list()

Posted by: Jordan in ProgrammingPHP on

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:


 
  1. $info = array('coffee', 'brown', 'caffeine');
  2.  
  3. // Listing all the variables
  4. list($drink, $color, $power) = $info;
  5. 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:


 
  1. $result = mysql_query("SELECT id, name, salary FROM employees", $conn);
  2. while (list($id, $name, $salary) = mysql_fetch_row($result)) {
  3. ....



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


 
  1. $myArray = array ( 'a'=>'v1',
  2.                     'b'=>'v2',
  3.                     'c'=>'v3'
  4.                    );
  5.  
  6. reset($array);
  7. while (list($key, $value) = each($myArray)) {
  8.       print "$key => $value" . PHP_EOL;
  9.         }



 
  1. $myArray = array ( 'a'=>'v1',
  2.  
  3.                     'b'=>'v2',
  4.  
  5.                     'c'=>'v3'
  6.  
  7.                    );
  8.  
  9.  
  10.         for (reset($array); list($key, $value) = each($array);) {
  11.              print "$key => $value" . PHP_EOL;
  12.         }



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 indexex time%
Start1215293315.27111400-0.00%
List1215293315.271917000.0008035.52%
Foreach1215293315.274089000.00217214.93%
While1215293315.276234000.00214514.74%
ForList1215293315.283232000.00699848.09%
For1215293315.285624000.00239216.44%
Stop1215293315.285666000.0000420.29%
total-0.014552100.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).


 
  1. require 'Benchmark/Timer.php';
  2.  
  3. // Create a new array
  4. $newArray = array ( 'a'=>'v1',
  5.                     'b'=>'v2',
  6.                     'c'=>'v3'
  7.                    );
  8.  
  9.    
  10.                    
  11. // Pass the param "true" to constructor to automatically display the results
  12. $timer = new Benchmark_Timer();
  13. $timer->start();
  14.  
  15. // Start with list
  16. loop_list($newArray);
  17. $timer->setMarker('List');
  18. $timer->timeElapsed('Start', 'List') . "n";
  19.  
  20. // Do foreach
  21. loop_foreach($newArray);
  22. $timer->setMarker('Foreach');
  23. $timer->timeElapsed('List', 'Foreach') . "n";
  24.  
  25. // Do foreach
  26. loop_foreach($newArray);
  27. $timer->setMarker('While');
  28. $timer->timeElapsed('ForEach', 'While') . "n";
  29.  
  30. // Do ForList
  31. loop_forlist($newArray);
  32. $timer->setMarker('ForList');
  33. $timer->timeElapsed('While', 'ForList') . "n";
  34.  
  35. // Do For
  36. loop_for($newArray);
  37. $timer->setMarker('For');
  38. $timer->timeElapsed('ForList', 'For') . "n";
  39.  
  40.  
  41.  
  42. // Stop the microtimer and
  43. // Display the Results      
  44. $timer->stop();
  45. $timer->display();
  46.  
  47.  
  48. /**
  49.  * Loop through an array using while list
  50.  *
  51.  * @param unknown_type $array
  52.  */
  53. function loop_list($array) {
  54.     reset($array);
  55.     for($i=0;$i<1000;$i++) {
  56.         while (list($key, $value) = each($array)) {
  57.         }
  58.     }
  59. }
  60.  
  61. /**
  62.  * Loop through an array using foreach
  63.  *
  64.  * @param unknown_type $array
  65.  */
  66. function loop_foreach($array) {
  67.     for($i=0;$i<1000;$i++) {
  68.         foreach($array as $key => $var) {
  69.         }
  70.     }
  71. }
  72.  
  73.  
  74.  
  75. /**
  76.  * Loop through an array using while
  77.  *
  78.  * @param unknown_type $array
  79.  */
  80. function loop_while($array) {
  81.     for($i=0;$i<1000;$i++) {
  82.         $p=0;
  83.         while ($p < count($array)) {
  84.             $p++;
  85.         }
  86.     }
  87. }
  88.  
  89.  
  90. /**
  91.  * Loop through array using "for"
  92.  * with list
  93.  *
  94.  * @param unknown_type $array
  95.  */
  96. function loop_forlist($array) {
  97.     for($i=0;$i<1000;$i++) {
  98.         for (reset($array); list($key, $value) = each($array);) {
  99.         }
  100.     }
  101. }
  102.  
  103. /**
  104.  * Loop through array using "for"
  105.  * without list
  106.  *
  107.  * @param unknown_type $array
  108.  */
  109. function loop_for($array) {
  110.     for($i=0;$i<1000;$i++) {
  111.         for ($p=0; $p<count($array); $p++)="" {="">
  112.         }
  113.     }
  114. }




Need Help?
Ask on our forum!



Trackback(0)
Comments (0)add comment

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger

busy