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/n/127133.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
D5QWBD5QWB
上一篇 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

发表回复

登录后才能评论