PHP-Array Functions -list(),each(), and count().

list() is sort of an operator, forming an lvalue (a value that can be used on the left side of an expression) out of a set of variables, which represents itself as a new entity similar to an element of a multidimensional array. As arguments, it takes a list of variables. When something is being assigned to it (a list of variables or an array element), the list of variables given as arguments to the list() operator is parsed from left to right. These arguments are then assigned a corresponding value from the rvalue (a value that's used on the right side of an expression). This is best explained using a code example:

$result = mysql_db_query($mysql_handle, $mysql_db,
                         "SELECT car_type, car_color, car_speed
                          FROM cars WHERE car_id=$car_id);

list($car_type, $car_color, $car_speed) = mysql_fetch_row($result);
Note: This code is used here strictly for example. It's not a good idea to implement code like this in real-life programs, as it relies on the fields remaining in the same order. If you change the field ordering, you have to change the variable ordering in the list() statement as well. Using associative arrays and manual value extraction imposes an overhead in programming but results in better code stability. Code as shown above should only be implemented for optimization purposes.

$my_array = array("Element 1", "Element 2", "Element 3");

while(list($key, $value) = each($my_array))
    print("Key: $key, Value: $value<br>");
 
 
You can also use each() to show the elements that each() itself returns:

$my_array = array("Element 1", "Element 2", "Element 3");

while($four_element_array = each($my_array))
{
   while(list($key, $value) = each($four_element_array))
       print("Key $key, Value $value<br>");
}
Using a simple for() loop is not enough for arrays of this kind; it accesses indices that don't have a value assigned. If PHP provides a stricter environment, this would result in an exception and immediate termination of the script. Therefore, whenever you're unsure about the contents and consistency of arrays, you're doomed to using each().

my array = array(0 => "Landon", 3 => "Graeme", 4 => "Tobias", 10 => "Till"); 
 for($i = 0; $i < count($my_array); $i++) 
print("Element $i: $my_array[$i]<br>");
Related Posts:
  • php.ini Settings for Session Management Before you get started with this chapter, you may have to make a couple of minor changes to your php.ini file so that sessions work correctly. On Windows If you are using a Windows version of PHP, the first thing you… Read More
  • HTTP Request Methods-PHP !--#brandmenu { background: none repeat scroll 0 0 #f6f6f6; border-bottom: 2px solid #777; /*clear: left;*/ float: left; margin-top: 3px; padding: 0; position: relative; width: 78px; min-he… Read More
  • Manage Databases on a Server by php MySQL-related functions. mysql_list_dbs() Used to list the databases on a MySQL server. mysql_num_rows() Returns the number of rows in a result set. mysql_tablename() Despite its name, can extract the name of … Read More
  • Visualize Traffic with DIY Vector you will learn how to create your own traffic chart using the incredibly cool Canvas framework, which can produce vector graphics and animations with a little bit of HTML and JavaScript. All code referenced in this hack i… Read More
  • codeigniter routes page The  routes.php filelets you remap URI requests to specific controller functions. For example, you may have a controller named sitewith a function named index. The URI for this controller/function combination might be… Read More
  • what is CodeIgniter Helpers? Helpers, as their name implies, help you with specific tasks. Unlike libraries, helpers are not object -oriented but procedural in nature. Each helper contains one or more functions, each focusing on a specific task, with ze… Read More
  • How to Enable mod_rewrite at Server mod_rewrite may not be enabled by default in your Apache configuration. To make sure,open the Apache configuration file, named httpd.conf. The full path of the file will be \Program Files\xampp\apache\conf\httpd.conf. Open … Read More
  • var_dump and print_r -PHP-standard Functions like var_dump and print_r are also invaluable when debugging var_dump var_dump functions displays information about variables in a simple, readable  format. This function is very useful when debugging—p… Read More
  • PHPGTK-application window PHPGTK is an extension to PHP that allows you to create graphical user interface (GUI) applications. Instead of running in a browser, your PHP application runs in its own application window. These applications are clie… Read More
  • top Database management system-PHP PHP Support for Multiple Databases  If you have been playing with PHP for a while, you have most likely noticed its excellent support for connecting to MySQL databases. Most of the PHP books on the market describ… Read More
  • URL rewriting-various exercises-seo  Installing mod_rewrite Testing mod_rewrite Working with regular expressions Rewriting numeric URLs with two parameters Rewriting keyword-rich URLs Building a link factory Pagination and UR… Read More
  • PHP-MySQL application Security With these two methods, there’s no longer any need to ever use GET for requests internal to an application. You may still need it for external requests, to other applications and web sites that aren’t coded to look for their… Read More
  • configuring PHP-impact security The primary mechanism for configuring PHP is the php.inifile. As the master file, this provides you with control over all configuration settings. Entries generally take the format:setting= value Be sure to read the comment… Read More
  • Advantages of MySQL and PHP Certain technologies play together better than others. PHP, a simple and powerful scripting language, and MySQL, a solid and reliable database server, make a perfect marriage between two modern technologies for building da… Read More
  • PHP while Loop PHP while Loop with code while - loops run  a set of code as  the  condition is true. Basic Syntaxwhile (condition){    code for executed;}<?php$k=1;while($k<=5) {  echo "The numbe… Read More