This site uses cookies from Google to deliver its services, to personalize ads and to analyze traffic. Information about your use of this site is shared with Google. By using this site, you agree to its use of cookies. Learn More

Php-array Functions -list(),each(), And Count().

Php-array Functions -list(),each(), And Count(). Information,Tech New Php-array Functions -list(),each(), And Count().

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

0 Response to "Php-array Functions -list(),each(), And Count()."

Posting Komentar

Contact

Nama

Email *

Pesan *