PHP each () and list () function

Source: Internet
Author: User
Tags arrays numeric mixed


void list (mixed varname, mixed ...)
Note: List () can only be used for arrays of numeric indices and assumes that the numeric index starts at 0

Example

  code is as follows copy code


. PHP

$info = array (' coffee ', ' brown ', ' caffeine ');

//Listing All of the Variables
list ($drink, $color, $power) = $info;
Echo $drink is $color and $power makes it s PECIAL.N ";

//Listing Some of them
list ($drink, $power) = $info;
Echo $drink has $power. N ";

//Let's skip to only the third one
list (,, $power) = $info;
echo "I need $power!n";

//List () doesn ' t work with strings
List ($bar) = "ABCDE";
Var_dump ($bar);//NULL
?


The each () function generates an array of key names and key values of the elements that the array's current internal pointer points to, and moves the internal pointer forward.
Array each (array &array)
Returns the key/value pair of the current pointer position in array arrays and moves the array pointer forward. A key-value pair is returned as an array of four cells, with the key named 0,1,key and value. The unit 0 and key contain the key names of the array cells, and 1 and value contain the data.

The code is as follows Copy Code

?
$people = Array ("Peter", "Joe", "Glenn", "Cleveland");
Print_r (each ($people));
?>


Each () is used in conjunction with the list () to traverse the array.

For example:

  code is as follows copy code

<?php
$cities =array ("California" =>array ("Martinez", "San Francisco", "Los Angeles"),
               "New York" =>array ("New York", "Buffalo")
            );
while (list ($key, $value) =each ($cities)
{   //echo $key;
//echo "Fdash";
//echo
while (list ($key 0, $val) =each ($value)) {
echo elements: $key 0,value: $val <br>n ";
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.