歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> PHP each() 函數用法簡介

PHP each() 函數用法簡介

日期:2017/3/1 9:06:20   编辑:Linux編程
  • each 函數用法簡介
  • 驗證each 函數對數組指針的操作影響
  • 個人小結

each 函數簡介

(PHP 4, PHP 5, PHP 7)
each — 返回數組中當前的鍵/值對並將數組指針向前移動一步

array each ( array &$array )

返回 array 數組中當前指針位置的鍵/值對並向前移動數組指針。鍵值對被返回為四個單元的數組,鍵名為0,1,key和 value。單元 0 和 key 包含有數組單元的鍵名,1 和 value 包含有數據。

如果內部指針越過了數組的末端,則 each() 返回 FALSE。

通常在執行 each() 之後,數組指針將停留在數組中的下一個單元或者當碰到數組結尾時停留在最後一個單元。如果要再用 each 遍歷數組,必須使用 reset()。

驗證each 函數對數組指針的操作影響

測試環境:

OS : CentOS 6.5
PHP: php 5.6
運行模式: cli

DEMO 1: 數組內部指針越過末端再次執行each返回false

  1 <?php
  2 /**
  3  * @description: php each 循環
  4  * 
  5  * @author : snow wolf <[email protected]>
  6  *
  7  * @date   : 2016-07-19
  8  */
  9 
 10 /* each 作用是返回當前數組的鍵值對,並將當前數組的指針向下移動一步,若內部指針越過了數組的末端返回false */
 11 
 12 $arr = range(1,2);
 13 
 14 while($list = each($arr))
 15 {
 16     var_dump($list);
 17     sleep(1);
 18 }
 19 
 20 //因數組指針到達末端,返回false 所以未執行 第二次循環
 21 while($list = each($arr))
 22 {
 23     var_dump($list);
 24     sleep(1);
 25 }
 26 
 27 
 28 echo '指針是否越過數組末端: ';
 29 var_dump(each($arr));
 30 2

執行結果:

 /data/phpLoop/each.php:16:
array(4) {
  [1] =>
  int(1)
  'value' =>
  int(1)
  [0] =>
  int(0)
  'key' =>
  int(0)
}
/data/phpLoop/each.php:16:
array(4) {
  [1] =>
  int(2)
  'value' =>
  int(2)
  [0] =>
  int(1)
  'key' =>
  int(1)
}

指針是否越過數組末端: /data/phpLoop/each.php:29:
bool(false)

DEMO2: 驗證,數組賦值是否會重置原來的數組指針

     $tmpArr = $arr;
 33 
 34 while($list = each($arr))
 35 {   
 36     var_dump($list);
 37     sleep(1);
 38 }
 39 echo '數組賦值測試指針是否越過數組末端: ';
 40 var_dump(each($arr));

執行結果:

   /data/phpLoop/each.php:35:
array(4) {
  [1] =>
  int(1)
  'value' =>
  int(1)
  [0] =>
  int(0)
  'key' =>
  int(0)
}
/data/phpLoop/each.php:35:
array(4) {
  [1] =>
  int(2)
  'value' =>
  int(2)
  [0] =>
  int(1)
  'key' =>
  int(1)
}
\n 數組賦值測試指針是否越過數組末端: 
/data/phpLoop/each.php:42:
array(4) {
  [1] =>
  int(1)
  'value' =>
  int(1)
  [0] =>
  int(0)
  'key' =>
  int(0)
}

DEMO3: 全局化 arr 數組,無形參,無傳值,測試是否重置指針

     /*全局化 arr 數組,無形參,無傳值,測試是否重置指針*/
 48 function doEach()
 49 {
 50     global $arr;
 51     while($list = each($arr))
 52     {
 53         var_dump($list);
 54         sleep(1);
 55     }
 56 
 57 }
 58 
 59 doEach();
 60 doEach();
 61 
 62 echo '\n 函數調用測試指針是否越過數組末端: ';
 63 var_dump(each($arr));

執行結果:

   /data/phpLoop/each.php:53:
array(4) {
  [1] =>
  int(1)
  'value' =>
  int(1)
  [0] =>
  int(0)
  'key' =>
  int(0)
}
/data/phpLoop/each.php:53:
array(4) {
  [1] =>
  int(2)
  'value' =>
  int(2)
  [0] =>
  int(1)
  'key' =>
  int(1)
}
\n 函數調用測試指針是否越過數組末端: /data/phpLoop/each.php:63:
bool(false)

DEMO4: 直接檢測 函數方式,是否越過數組末端

   /*直接檢測 函數方式,是否越過數組末端 */
 66 
 67 function funEach($arr)
 68 {
 69     while($list = each($arr))
 70     {
 71         var_dump($list);
 72         sleep(1);
 73     }
 74 }
 75 
 76 funEach($arr);
 77 funEach($arr);
 78 
 79 echo '\n 函數直接調用測試指針是否越過數組末端: ';
 80 var_dump(each($arr));

執行結果:

   /data/phpLoop/each.php:71:
array(4) {
  [1] =>
  int(1)
  'value' =>
  int(1)
  [0] =>
  int(0)
  'key' =>
  int(0)
}
/data/phpLoop/each.php:71:
array(4) {
  [1] =>
  int(2)
  'value' =>
  int(2)
  [0] =>
  int(1)
  'key' =>
  int(1)
}
/data/phpLoop/each.php:71:
array(4) {
  [1] =>
  int(1)
  'value' =>
  int(1)
  [0] =>
  int(0)
  'key' =>
  int(0)
}
/data/phpLoop/each.php:71:
array(4) {
  [1] =>
  int(2)
  'value' =>
  int(2)
  [0] =>
  int(1)
  'key' =>
  int(1)
}
\n 函數直接調用測試指針是否越過數組末端: /data/phpLoop/each.php:80:
array(4) {
  [1] =>
  int(1)
  'value' =>
  int(1)
  [0] =>
  int(0)
  'key' =>
  int(0)
}

個人小結

在執行 each() 之後,數組指針將停留在數組中的下一個單元或者當碰到數組結尾時停留在最後一個單元,但如果此時對當前數組賦值則會重置原來的數組指針,因此若在循環中需小心使用each 勿賦值,否則將會導致無限循環。

Copyright © Linux教程網 All Rights Reserved