我是靠谱客的博主 重要冬天,这篇文章主要介绍ArrayIterator迭代器遍历数组代码运行结果,现在分享给大家,希望可以做个参考。

代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php namespace appindexcontroller; 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/>"; } } }

运行结果

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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

最后

以上就是重要冬天最近收集整理的关于ArrayIterator迭代器遍历数组代码运行结果的全部内容,更多相关ArrayIterator迭代器遍历数组代码运行结果内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(94)

评论列表共有 0 条评论

立即
投稿
返回
顶部