php中的iterator(「PHP」)

  • 1、如何RecursiveIteratorIterator在PHP中工作
  • 2、php迭代器iterator怎麼用
  • 3、什麼是迭代器 iterator

Some speed tests

?php

$timer = function ($name = ‘default’, $unset_timer = TRUE)

{

static $timers = array();

if ( isset( $timers[ $name ] ) )

{

list($s_sec, $s_mic) = explode(‘ ‘, $timers[ $name ]);

list($e_sec, $e_mic) = explode(‘ ‘, microtime());

if ( $unset_timer )

unset( $timers[ $name ] );

return $e_sec – $s_sec + ( $e_mic – $s_mic );

}

$timers[ $name ] = microtime();

};

function f1 ($array) {

$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($array), RecursiveIteratorIterator::SELF_FIRST);

foreach ( $iterator as $key = $value ) {

if ( is_array($value) )

continue;

}

}

function f2($array) {

foreach ( $array as $key = $value ) {

if ( is_array($value) )

f2($value);

}

}

foreach ( [100, 1000, 10000, 100000, 1000000] as $num )

{

$array = [];

for ( $i = 0; ++$i $num; )

$array[] = [1,2,3=[4,5,6=[7,8,9=10,11,12=[13,14,15=[16,17,18]]]]];

$timer();

f1($array);

printf(“RecursiveIteratorIterator: %7d elements – %.3f sec\n”, $num, $timer());

$timer();

f2($array);

printf(“Recursive function : %7d elements – %.3f sec\n”, $num, $timer());

}

?

Output (PHP 5.4.9-4ubuntu2.1 (cli) (built: Jun 11 2013 13:10:01))

=======================

RecursiveIteratorIterator: 100 elements – 0.007 sec

Recursive function : 100 elements – 0.002 sec

RecursiveIteratorIterator: 1000 elements – 0.036 sec

Recursive function : 1000 elements – 0.024 sec

RecursiveIteratorIterator: 10000 elements – 0.425 sec

Recursive function : 10000 elements – 0.263 sec

RecursiveIteratorIterator: 100000 elements – 8.153 sec

Recursive function : 100000 elements – 2.654 sec

RecursiveIteratorIterator: 1000000 elements – 474.483 sec

Recursive function : 1000000 elements – 26.872 sec

For one million elements recursive function is more quickly!

up

down

7 Adil Baig @ AIdezigns ¶5 years ago

A very important thing to note about \RecursiveIteratorIterator is that it returns a flattened array when used with the iterator_to_array function. Ex:

?php

$arr = array(‘Zero’, ‘name’=’Adil’, ‘address’ = array( ‘city’=’Dubai’, ‘tel’ = array(‘int’ = 971, ‘tel’=12345487)), ” = ‘nothing’);

$iterator = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($arr));

var_dump(iterator_to_array($iterator,true));

?

This code will return :

array(6) {

[0]=

string(4) “Zero”

[“name”]=

string(4) “Adil”

[“city”]=

string(5) “Dubai”

[“int”]=

int(91)

[“tel”]=

int(12345487)

[“”]=

string(7) “nothing”

}

To get the non-flattened proper array use the getArrayCopy() method, like so :

$iterator-getArrayCopy()

This will return

array(4) {

[0]=

string(4) “Zero”

[“name”]=

string(4) “Adil”

[“address”]=

array(2) {

[“city”]=

string(5) “Dubai”

[“tel”]=

array(2) {

[“int”]=

int(91)

[“tel”]=

int(12345487)

}

}

[“”]=

string(7) “nothing”

}

up

down

6 aidan at php dot net ¶6 years ago

This example demonstrates using the getDepth() method with a RecursiveArrayIterator.

?php

$tree = array();

$tree[1][2][3] = ‘lemon’;

$tree[1][4] = ‘melon’;

$tree[2][3] = ‘orange’;

$tree[2][5] = ‘grape’;

$tree[3] = ‘pineapple’;

print_r($tree);

$arrayiter = new RecursiveArrayIterator($tree);

$iteriter = new RecursiveIteratorIterator($arrayiter);

foreach ($iteriter as $key = $value) {

$d = $iteriter-getDepth();

echo “depth=$d k=$key v=$value\n”;

}

?

The output of this would be:

Array

(

[1] = Array

(

[2] = Array

(

[3] = lemon

)

[4] = melon

)

[2] = Array

(

[3] = orange

[5] = grape

)

[3] = pineapple

)

depth=2 k=3 v=lemon

depth=1 k=4 v=melon

depth=1 k=3 v=orange

depth=1 k=5 v=grape

depth=0 k=3 v=pineapple

up

down

4 fengdingbo at gmail dot com ¶3 years ago

if you want traversal directory。

?php

foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator(“./”)) as $key=$val)

{

echo $key,”=”,$val,”\n”;

}

?

up

down

8 Michiel Brandenburg ¶7 years ago

You can use this to quickly find all the files (recursively) in a certain directory. This beats maintaining a stack yourself.

?php

$directory = “/tmp/”;

$fileSPLObjects = new RecursiveIteratorIterator(

new RecursiveDirectoryIterator($directory),

RecursiveIteratorIterator::CHILD_FIRST

);

try {

foreach( $fileSPLObjects as $fullFileName = $fileSPLObject ) {

print $fullFileName . ” ” . $fileSPLObject-getFilename() . “\n”;

}

}

catch (UnexpectedValueException $e) {

printf(“Directory [%s] contained a directory we can not recurse into”, $directory);

}

?

Note: if there is a directory contained within the directory you are searching in that you have no access to read an UnexpectedValueException will be thrown (leaving you with an empty list).

Note: objects returned are SPLFileObjects

up

down

1 gerry at king-foo dot be ¶2 years ago

Carefull when using iterator_to_array(). Because it flattens down your subiterators, elements with the same keys will overwrite eachother.

For example:

?php

$iterator = new RecursiveIteratorIterator(

new RecursiveArrayIterator([

[‘foo’, ‘bar’],

[‘baz’, ‘qux’]

])

);

foreach ($iterator as $element) {

echo $element;

}

?

This will output all 4 elements as expected:

string(3) “foo”

string(3) “bar”

string(3) “baz”

string(3) “qux”

While doing:

?php

var_dump(iterator_to_array($iterator));

?

will output an array with only the last 2 elements:

array(2) {

[0]=

string(3) “baz”

[1]=

string(3) “qux”

}

使用foreach 與使用迭代器,並不衝突 

迭代器可以使用在:

1、使用返回迭代器的包或庫時(如PHP5中的SPL迭代器)

2、無法在一次的調用獲取容器的所有元素時

3、要處理數量巨大的無素時(數據庫中的表以GB計的數據)

迭代器還可以用來構造一些數據結構。

你可以去後盾人平台看看,裏面的東西不錯

迭代器(iterator)有時又稱游標(cursor)是程序設計的軟件設計模式,可在容器(container,例如鏈表或陣列)上遍訪的接口,設計人員無需關心容器的內容。

為了方便的處理集合中的元素,Java中出現了一個對象,該對象提供了一些方法專門處理集合中的元素.

例如刪除和獲取集合中的元素.該對象就叫做迭代器(Iterator).

對 Collection 進行迭代的類,稱其為迭代器。還是面向對象的思想,專業對象做專業的事情,迭代器就是專門取出集合元素的對象。

但是該對象比較特殊,不能直接創建對象(通過new),該對象是以內部類的形式存在於每個集合類的內部。

原創文章,作者:D5QWB,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/127133.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
D5QWB的頭像D5QWB
上一篇 2024-10-03 23:13
下一篇 2024-10-03 23:13

相關推薦

  • PHP和Python哪個好找工作?

    PHP和Python都是非常流行的編程語言,它們被廣泛應用於不同領域的開發中。但是,在考慮擇業方向的時候,很多人都會有一個問題:PHP和Python哪個好找工作?這篇文章將從多個方…

    編程 2025-04-29
  • PHP怎麼接幣

    想要在自己的網站或應用中接受比特幣等加密貨幣的支付,就需要對該加密貨幣擁有一定的了解,並使用對應的API進行開發。本文將從多個方面詳細闡述如何使用PHP接受加密貨幣的支付。 一、環…

    編程 2025-04-29
  • 使用PHP foreach遍歷有相同屬性的值

    本篇文章將介紹如何使用PHP foreach遍歷具有相同屬性的值,並給出相應的代碼示例。 一、基礎概念 在講解如何使用PHP foreach遍歷有相同屬性的值之前,我們需要先了解幾…

    編程 2025-04-28
  • PHP獲取301跳轉後的地址

    本文將為大家介紹如何使用PHP獲取301跳轉後的地址。301重定向是什麼呢?當我們訪問一個網頁A,但是它已經被遷移到了另一個地址B,此時若服務器端做了301重定向,那麼你的瀏覽器在…

    編程 2025-04-27
  • PHP登錄頁面代碼實現

    本文將從多個方面詳細闡述如何使用PHP編寫一個簡單的登錄頁面。 1. PHP登錄頁面基本架構 在PHP登錄頁面中,需要包含HTML表單,用戶在表單中輸入賬號密碼等信息,提交表單後服…

    編程 2025-04-27
  • PHP與Python的比較

    本文將會對PHP與Python進行比較和對比分析,包括語法特性、優缺點等方面。幫助讀者更好地理解和使用這兩種語言。 一、語法特性 PHP語法特性: <?php // 簡單的P…

    編程 2025-04-27
  • PHP版本管理工具phpenv詳解

    在PHP項目開發過程中,我們可能需要用到不同版本的PHP環境來試驗不同的功能或避免不同版本的兼容性問題。或者我們需要在同一台服務器上同時運行多個不同版本的PHP語言。但是每次手動安…

    編程 2025-04-24
  • PHP數組去重詳解

    一、array_unique函數 array_unique是php中常用的數組去重函數,它基於值來判斷元素是否重複,具體使用方法如下: $array = array(‘a’, ‘b…

    編程 2025-04-24
  • PHP導出Excel文件

    一、PHP導出Excel文件列寬調整 當我們使用PHP導出Excel文件時,有時需要調整單元格的列寬。可以使用PHPExcel類庫中的setWidth方法來設置單元格的列寬。下面是…

    編程 2025-04-24
  • php擴展庫初探

    一、什麼是php擴展庫? PHP擴展庫(PHP extension)是一些用C語言編寫的動態鏈接庫,用於擴展PHP的功能。PHP擴展庫使得PHP可以與各種數據庫系統相連、SMTP、…

    編程 2025-04-23

發表回復

登錄後才能評論