文章總結一下關于PHP each()與list()函數 有需要的朋友可參考一下。
void list ( mixed varname, mixed … )
注:list() 僅能用于數字索引的數組并假定數字索引從 0 開始.
例子,代碼如下:
- <?php
- $info = array('coffee', 'brown', 'caffeine');
- // Listing all the variables
- list($drink, $color, $power) = $info;
- echo "$drink is $color and $power makes it special.n";
- // Listing some of them
- list($drink, , $power) = $info;
- echo "$drink has $power.n";
- // Or 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
- ?>
each() 函數生成一個由數組當前內部指針所指向的元素的鍵名和鍵值組成的數組,并把內部指針向前移動.
array each ( array &array )
返回 array 數組中當前指針位置的鍵/值對并向前移動數組指針,鍵值對被返回為四個單元的數組,鍵名為 0,1,key 和 value,單元 0 和 key 包含有數組單元的鍵名,1 和 value 包含有數據,代碼如下:
- <?php
- $people = array("Peter", "Joe", "Glenn", "Cleveland");
- print_r (each($people));
- ?>
each() 經常和 list() 結合使用來遍歷數組,例如,代碼如下:
- <?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($key0,$val)=each($value)){
- echo "elements:$key0,value:$val<br>n";
- }
- }
- ?>
新聞熱點
疑難解答