Thursday, 25 September 2014

each in PHP

PHP each() function is utilized to get present element's key and value from an array, and moves the internal pointer forward.
PHP each() function returns FALSE if there are no more array elements.

Syntax:

each(array)
array : Required. Specifies the array to use.
Note : PHP each() function returns FALSE on empty elements or elements with no value.

Example:

<?php
$daysarr = array( "Sunday","Monday","Tuesday","Wednesday","Thursday" );
print_r( each($daysarr) );
echo "<br />"."same example with a loop to output the whole array:";
$daysarr = array( "Sunday","Monday","Tuesday","Wednesday","Thursday" );
reset($daysarr);
while( list($key,$val) = each($daysarr) )
{
            echo "$key => $val"."<br />";
}
?>

Output will be:

Array ( [1] => Sunday [value] => Sunday [0] => 0 [key] => 0 )
same example with a loop to output the whole array:
 0 => Sunday
 1 => Monday 
 2 => Tuesday 
 3 => Wednesday 
 4 => Thursday

0 comments:

Post a Comment