$people = array("Bill", "Steve", "Mark", "David");
echo current($people) . "
"; // 当前元素是 Bill
echo next($people) . "
"; // Bill 的下一个元素是 Steve
echo current($people) . "
"; // 现在当前元素是 Steve
echo prev($people) . "
"; // Steve 的上一个元素是 Bill
echo end($people) . "
"; // 最后一个元素是 David
echo prev($people) . "
"; // David 之前的元素是 Mark
echo current($people) . "
"; // 目前的当前元素是 Mark
echo reset($people) . "
"; // 把内部指针移动到数组的首个元素,即 Bill
echo next($people) . "
"; // Bill 的下一个元素是 Steve
print_r (each($people)); // 返回当前元素的键名和键值(目前是 Steve),并向前移动内部指针
?>
然后是SPL标准库的一些测试
//迭代器
$fruits = [
"apple" => 'apple value', //position = 0
"orange" => 'orange value', //position = 1
"grape" => 'grape value',
"plum" => 'plum value',
];
echo "**** use fruits directly *******\n";
foreach ($fruits as $key => $value) {
echo $key . ":" .$value. "\n";
}
//我们经常用的foreach的原理是下面的这部分。
echo "**** use ArrayIterator in for *******\n";
// 使用ArrayIterator遍历数组, 先把数组包装成一个ArrayObject,然后去生成迭代器
$obj = new ArrayObject($fruits);
$it = $obj->getIterator();
foreach ($it as $key => $value) {
echo $key . ":" .$value. "\n";
}
echo "**** use ArrayIterator in while *******\n";
$it->rewind(); //别忘了rewind
while($it->valid()) {
echo $it->key() . ":" .$it->current(). "\n";
$it->next();
}
echo "**** use ArrayIterator jump someone *******\n\n";
//跳过某些元素进行打印
$it->rewind();
if($it->valid()){
$it->seek(2); //找到position = 2的节点,跳过之前的,调用seek方法,要先调用valid
while($it->valid()) {
echo $it->key() . ":" .$it->current(). "\n";
$it->next();
}
}
echo "**** use ArrayIterator sort *******\n\n";
$it->ksort(); //根据key排序
foreach ($it as $key => $value) {
echo $key . ":" .$value. "\n";
}
echo "**** use ArrayIterator sort *******\n\n";
$it->asort(); //根据value排序
foreach ($it as $key => $value) {
echo $key . ":" .$value. "\n";
}
?>
//双向链表
$obj = new SplDoublyLinkedList();
$obj->push(1); //把新的节点数据添加到数据链表的顶部(Top)
$obj->push(2);
$obj->push(3);
$obj->unshift(10);//把新的节点数据添加到数据链表的底部(Bottom)
print_r($obj);
$obj->rewind(); //rewind用于把节点指针指向bottom所在节点
echo 'current:'. $obj->current()."\n"; //current获取指针指向的节点
$obj->next();
echo 'next node:'. $obj->current()."\n";
$obj->next();//指针指向下一个节点
$obj->next();
$obj->prev();//指针指向上一个节点
echo 'prev node:'. $obj->current()."\n";
$obj->next();
$obj->next();
echo 'next node:'. $obj->current()."\n";
if($obj->current())
echo "Current node valid\n";
else
echo "Current node invalid\n";
$obj->rewind();
if($obj->valid()) //如果当前节点是有效节点,valid返回true
echo "valid list\n";
else
echo "invalid list\n";
echo "Pop value:". $obj->pop()."\n";
print_r($obj);
echo 'current:'. $obj->current()."\n";
$obj->next();//1
$obj->next();//2(top)
$obj->pop(); //1.把Top位置的节点从链表中删除,并返回。
//如果current正好指向Top位置,那么调用pop之后,current会失效。
echo 'next node:'.$obj->current()."\n";
print_r($obj);
$obj->shift();//把Bottom位置的节点从链表中删除,并返回。
print_r($obj);
$queue = new SplQueue(); //队列
$queue->enqueue('a'); // 插入一个节点到队列里面的Top位置
$queue->enqueue('b');
$queue->enqueue('c');
print_r($queue);
echo "Bottom:" . $queue->bottom() . "\n"; //队列头
echo "Top:" . $queue->top() . "\n"; //队列尾
//队列的offset=0是Bottom所在的位置,offset=1往下找
$queue->offsetSet(0,'A');
print_r($queue);
//双向列表的rewind和堆栈的rewind相反,堆栈的rewind使得当前指针指向Top所在的位置,而双向链表的rewind使得当前指针指向Bottom所在的位置,队列跟双向链表一样。
$queue->rewind();
echo "current: " . $queue->current() . "\n";
while ($queue->valid()) {
echo $queue->key()."=>".$queue->current(). "\n";
$queue->next();
}
//dequeue类似堆栈的pop,提取bottom位置的节点,并返回。同时,删除这个元素
echo "dequeue queue:" . $queue->dequeue() . "\n";
print_r($queue);
?>
//堆栈
$stack = new SplStack();
//不管是双向链表还是堆栈,rewind都指向最先出来的那个节点。
$stack->push('a'); //向堆栈中放入一个元素到Top位置
$stack->push('b');
$stack->push('c');
echo "Bottom:" . $stack->bottom() . "\n"; //栈底
echo "Top:" . $stack->top() . "\n"; //栈顶
//堆栈的offset=0是Top所在的位置,offset=1往上找
$stack->offsetSet(0,'C');
print_r($stack);
//双向列表的rewind和堆栈的rewind相反,堆栈的rewind使得当前指针指向Top所在的位置,而双向链表的rewind使得当前指针指向Bottom所在的位置。
$stack->rewind();
echo "current:" . $stack->current() . "\n";
//堆栈的next往下走(往Bottom方向)
$stack->next();
echo "next:" . $stack->current() . "\n";
// 遍历堆栈,先入后出
$stack->rewind();
while ($stack->valid()) {
echo $stack->key()."=>" . $stack->current()."\n";
$stack->next(); //next操作不会从链表中删除元素
}
// 删除堆栈数据
$popObj = $stack->pop(); //pop操作从堆栈中提取出最后一个元素(Top位置),同时从堆栈中删除该节点
echo "Poped object: " . $popObj . "\n";
print_r($stack);
$a = false || true;
$b = false or true;
?>