ArrayIterator迭代器遍历数组

29 篇文章 0 订阅
19 篇文章 1 订阅

代码

<?php
namespace app\index\controller;
use ArrayObject;//引入迭代器
class Index
{
    public function index()
    {
        $fruits = array(
            "apple" => 'apple value',//position =0
            "orange" => 'orange value',//position =1
            "grape" => 'grape value',
            "plum" => 'plum value',
        );

        dump($fruits);

        echo '------------------普通数组遍历-----------------'."<br/>";

        foreach ($fruits as $key => $value) {
            echo $key.":".$value."<br/>";
        }

        echo '------------------使用ArrayIterator迭代器遍历数组(foreach)-----------------'."<br/>";
        $obj = new ArrayObject($fruits);//创建数组对象
        $it = $obj->getIterator();//获取迭代器

        foreach ($it as $key => $value) {
            echo $key.":".$value."<br/>";
        }

        echo '------------------(while)-----------------'."<br/>";

        $it->rewind();//如果要使用current必须使用rewind
        while ($it -> valid()) {
            echo $it->key().":".$it->current()."<br/>";
            $it -> next();
        }

        echo '------------------跳过某些元素进行打印(while)-----------------'."<br/>";

        $it->rewind();
        if($it->valid()){
            $it -> seek(1);//当前指针指向position=1
            while ($it -> valid()) {
                echo $it->key().":".$it->current()."<br/>";
                $it -> next();
            }
        }

        echo '------------------用迭代器的key进行排序(ksort)-----------------'."<br/>";
        $it->ksort();
        foreach ($it as $key => $value) {
            echo $key.":".$value."<br/>";
        }

        echo '------------------用迭代器的value进行排序(asort)-----------------'."<br/>";
        $it->asort();
        foreach ($it as $key => $value) {
            echo $key.":".$value."<br/>";
        }
    }   
}

运行结果

array(4) {
  ["apple"] => string(11) "apple value"
  ["orange"] => string(12) "orange value"
  ["grape"] => string(11) "brape value"
  ["plum"] => string(10) "plum value"
}
------------------普通数组遍历-----------------
apple:apple value
orange:orange value
grape:brape value
plum:plum value
------------------使用ArrayIterator迭代器遍历数组(foreach)-----------------
apple:apple value
orange:orange value
grape:brape value
plum:plum value
------------------(while)-----------------
apple:apple value
orange:orange value
grape:brape value
plum:plum value
------------------跳过某些元素进行打印(while)-----------------
orange:orange value
grape:brape value
plum:plum value
------------------用迭代器的key进行排序(ksort)-----------------
apple:apple value
grape:brape value
orange:orange value
plum:plum value
------------------用迭代器的value进行排序(asort)-----------------
apple:apple value
grape:brape value
orange:orange value
plum:plum value
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值