header("Content-type:text/html;charset=utf-8");
$student=array(
'wangyu'=>'男',
'tianjiao'=>'女',
'jianxin'=>'男',
'ahengyi'=>'未知'
);
//常规遍历
foreach ($student as $key => $value) {
echo $key.'---->'.$value.'<br/>';
}
//使用spl里的ArrayIterator迭代器遍历
$obj=new ArrayObject($student);
$stu=$obj->getIterator();
$stu->ksort(); //对键名进行排序
$stu->asort(); //对键值进行排序
// 注:此ksort,asort等方法并未实现指针排序,所以使用$stu->next()会有意想不到的结果
foreach ($stu as $key => $value) {
echo $key.'---->'.$value.'<br/>';
}
//使用while遍历
$stu->rewind(); //php5.3不用加,官方给出例子也未用
while($stu->valid()){
echo $stu->key().'---->'.$stu->current().'<br/>';
$stu->next();
}
php标准库 SPL 迭代器之 ---------ArrayIterator(数组迭代)
最新推荐文章于 2021-03-25 20:41:14 发布