zoukankan      html  css  js  c++  java
  • PHP SPL标准库-迭代器

    通过某种统一的方式遍历链表或者数组中的元素的过程叫做迭代遍历,这种统一的遍历工具我们叫做迭代器

     PHP中迭代器是通过Iterator 接口定义的。

    ArrayIterator迭代器

    foreach 默认实现了ArrayIterator接口,但是通过这个迭代器,可以实现更加高级的功能。

    • seek(),指针定位到某位置,跳过前面 n-1的元素
    • ksort(),对key进行字典序排序
    • asort(),对值进行字典序排序
    $arr=array(
        'apple' => 'apple value', // position = 0
        'orange' => 'orange value', // position = 1
        'grape' => 'grape value',
        'plum' => 'plum value'
    );
    $obj=new ArrayObject($arr);
    // 生成数组的迭代器
    $it =$obj->getIterator();
    foreach ($it as $key => $value){
        echo $key . ":". $value .'<br />';
    }
    
    echo '<br />';
    // 实现和foreach同样功能
    $it->rewind(); // 调用current之前一定要调用rewind
    While($it->valid()){ // 判断当前是否为有效数据
        echo $it->key().' : '.$it->current().'<br />';
        $it->next(); // 千万不能少
    }
    
    // 实现更复杂功能,跳过某些元素进行打印
    $it->rewind();
    if ($it->valid()){
        $it->seek(1); // position,跳过前面 n-1的元素
        While($it->valid()){// 判断当前是否为有效数据
            echo $it->key().' : '.$it->current().'<br />';
            $it->next(); // 千万不能少
        }
    }
    
    $it->ksort(); // 对key进行字典序排序
    //$it->asort(); // 对值进行字典序排序
    foreach ($it as $key => $value){
        echo $key . ":". $value .'<br />';
    }
    

    返回结果:

    apple:apple value
    orange:orange value
    grape:grape value
    plum:plum value
    
    apple : apple value
    orange : orange value
    grape : grape value
    plum : plum value
    
    orange : orange value
    grape : grape value
    plum : plum value
    
    apple:apple value
    grape:grape value
    orange:orange value
    plum:plum value

    AppendIterator迭代器

    AppendIterator能陆续遍历几个迭代器,按顺序迭代访问几个不同的迭代器。例如希望在一次循环中迭代访问两个或更多的组合

    $arr_a = new ArrayIterator(array('a'=> array('a','b'=>234),'b','c'));
    $arr_b = new ArrayIterator(array('d','e','f'));
    $it = new AppendIterator();
    $it->append($arr_a); // 追加数组
    $it->append($arr_b); // 追加数组,然后遍历$it
    foreach ($it as $key => $value){
    	print_r($key);
    	echo '-';
        print_r($value);
    	echo '<br />---------------<br />';
    }

    返回结果:

    a-Array ( [0] => a [b] => 234 ) 
    ---------------
    0-b
    ---------------
    1-c
    ---------------
    0-d
    ---------------
    1-e
    ---------------
    2-f
    ---------------

    MultipleIterator迭代器

    用于把多个Iterator 里面的数据组合成为一个整体来访问

    • Multipleiterator:将多个arrayiterator拼凑起来
    • Appenditerator:将多个arrayiteratorr连接起来
    $idIter = new ArrayIterator(array('01','02','03'));
    $nameIter = new ArrayIterator(array('张三','李四','王五'));
    $ageIter = new ArrayIterator(array('22','23','25'));
    $mit = new MultipleIterator(MultipleIterator::MIT_KEYS_ASSOC); // 按照key关联
    $mit->attachIterator($idIter,"ID");
    $mit->attachIterator($nameIter,"NAME");
    $mit->attachIterator($ageIter,"AGE");
    foreach ($mit as $value){
        print_r($value);
    }
    

    返回结果:

    Array
    (
        [ID] => 01
        [NAME] => 张三
        [AGE] => 22
    )
    Array
    (
        [ID] => 02
        [NAME] => 李四
        [AGE] => 23
    )
    Array
    (
        [ID] => 03
        [NAME] => 王五
        [AGE] => 25
    )
    

      

  • 相关阅读:
    D3D中的粒子系统(1)
    半角/全角的转换算法
    对Native API NtSystemDebugControl的分析
    教你快速下载fs2you.com网盘的文件
    养眼的编辑器配色
    Direct3D中的绘制(2)
    在服务里面弹出一个窗口到用户的桌面上[转]
    驱动级隐藏文件,注册表,进程
    GB2312转unicode程序
    Direct3D中的绘制(5)
  • 原文地址:https://www.cnblogs.com/xi-jie/p/10565910.html
Copyright © 2011-2022 走看看