jtahstu的博客

root@jtahstu.com   Github   英文博客  

最新碎语:以后没事写写小的知识点吧

您的位置:jtahstu的博客 >笔记> 标准PHP类库(SPL)之栈、队列、迭代器笔记

标准PHP类库(SPL)之栈、队列、迭代器笔记

SplStack.php


<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8"/>
        <title>
            SplStack
        </title>
        <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible" />
        <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
        <meta name="Keywords" content=""/>
        <meta name="Description" content=""/>
        <meta name="author" content="jtahstu"/>
        <!--<link rel="icon" href="http://cdn.jtahstu.com/favicon.ico" />
        <link href="http://apps.bdimg.com/libs/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" />
        <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
        <script src="http://apps.bdimg.com/libs/bootstrap/3.3.4/js/bootstrap.min.js"></script>-->
    </head>
    <body>
         <div class="container">
         	<pre>
            <?php
			    $stack=new SplStack();
				$stack->push('jt');
				$stack->push('ahstu');
				$stack->push('EDG');
				$stack->push('RNG');
				var_dump($stack);
				
				$stack->pop();
				var_dump($stack);
				
				echo 'bottom:'.$stack->bottom().'<br />';
				echo 'top:'.$stack->top().'<br />';
				
				//遍历堆栈
				$stack->rewind();
				while($stack->valid()){
					echo $stack->key().':'.$stack->current().'<br />';
					$stack->next();
				}
			?>
			</pre>
        </div>
    </body>
<ml>



SplQueue.php


<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8"/>
        <title>
            SqlQueue
        </title>
        <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible" />
        <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
        <meta name="Keywords" content=""/>
        <meta name="Description" content=""/>
        <meta name="author" content="jtahstu"/>
        <!--<link rel="icon" href="http://cdn.jtahstu.com/favicon.ico" />
        <link href="http://apps.bdimg.com/libs/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" />
        <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
        <script src="http://apps.bdimg.com/libs/bootstrap/3.3.4/js/bootstrap.min.js"></script>-->
    </head>
    <body>
         <div class="container">
         	<pre>
            <?php
                $q = new SplQueue();
				$q->enqueue(1);
				$q->enqueue(2);
				$q->enqueue(3);
				$q->pop();
				print_r($q);
				$q->offsetSet(0,'A');
				print_r($q);
				echo 'bottom:'.$q->bottom().'<br />';
				echo 'top:'.$q->top().'<br />';
				
				//遍历堆栈
				$q->rewind();
				while($q->valid()){
					echo $q->key().':'.$q->current().'<br />';
					$q->next();
				}
            ?>	
            </pre>
        </div>
    </body>
<ml>
SplIterator.php



<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8"/>
		<title> SplIterator </title>
		<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible" />
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<meta name="Keywords" content=""/>
		<meta name="Description" content=""/>
		<meta name="author" content="jtahstu"/>
		<!--<link rel="icon" href="http://cdn.jtahstu.com/favicon.ico" />
		<link href="http://apps.bdimg.com/libs/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" />
		<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
		<script src="http://apps.bdimg.com/libs/bootstrap/3.3.4/js/bootstrap.min.js"></script>-->
	</head>
	<body>
		<div class="container">
			<pre>
            		<?php
					$fr = array('a' => 'jt', 'b' => 'ahstu', 'c' => 'tt', 'd' => 'abc');
					print_r($fr);
					echo '<hr>';

					//使用ArrayIterator
					$obj = new ArrayObject($fr);
					$it = $obj -> getIterator();

					$it -> rewind();
					while ($it -> valid()) {
						echo $it -> key() . ':' . $it -> current() . '<br />';
						$it -> next();
					}
					echo '<hr>';

					//跳过某些元素进行打印
					$it -> rewind();
					if ($it -> valid()) {
						$it -> seek(1);
						//跳到数组下标为1的元素
						while ($it -> valid()) {
							echo $it -> key() . ':' . $it -> current() . '<br />';
							$it -> next();
						}
					}
					echo '<hr>';
					
//					$it->ksort();	//按key来排序
					$it->asort();	//按value来排序
					foreach($it as $key=>$value){
						echo $key . ':' . $value . '<br />';
					}
            		?>
            </pre>
		</div>
	</body>
	<ml>
MultipleIterator.php



<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8"/>
		<title></title>
		<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible" />
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<meta name="Keywords" content=""/>
		<meta name="Description" content=""/>
		<meta name="author" content="jtahstu"/>
		<!--<link rel="icon" href="http://cdn.jtahstu.com/favicon.ico" />
		<link href="http://apps.bdimg.com/libs/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" />
		<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
		<script src="http://apps.bdimg.com/libs/bootstrap/3.3.4/js/bootstrap.min.js"></script>-->
	</head>
	<body>
		<div class="container">
			<pre>
				<?php

				$it1 = new ArrayIterator( array('jin', 'wang', 'zhang'));
				$it2 = new ArrayIterator( array('tao', 'ba', 'x'));

				$multipleIterator = new MultipleIterator(MultipleIterator::MIT_NEED_ALL | MultipleIterator::MIT_KEYS_ASSOC);

				$multipleIterator -> attachIterator($it1, '姓');
				$multipleIterator -> attachIterator($it2, '名');

				foreach ($multipleIterator as $key => $value) {
//					echo "Key\n";
//					var_dump($key);
					echo "Value\n";
					var_dump($value);
					echo "---next---\n";
				}
				var_dump($multipleIterator);
			?>
			</pre>
		</div>
	</body>
</html>
FilesystemIterator.php



<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8"/>
		<title> test </title>
		<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible" />
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<meta name="Keywords" content=""/>
		<meta name="Description" content=""/>
		<meta name="author" content="jtahstu"/>
		<!--<link rel="icon" href="http://cdn.jtahstu.com/favicon.ico" />
		<link href="http://apps.bdimg.com/libs/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" />
		<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
		<script src="http://apps.bdimg.com/libs/bootstrap/3.3.4/js/bootstrap.min.js"></script>-->
	</head>
	<body>
		<div class="container">

            	<?php
            	    $it=new FilesystemIterator('.');
				foreach($it as $finfo){
					printf("%s %s  %8s  %s<br />",date("Y-m-d H:i:s",$finfo->getMTime()),
						$finfo->isDir()?'<DIR>':'',
						number_format($finfo->getSize()),
                    		$finfo->getFilename()
					);
				}
            	?>

		</div>
	</body>
</html>


---

本文章采用 知识共享署名2.5中国大陆许可协议 进行许可,欢迎转载,演绎或用于商业目的。

---

二维码加载中...

扫一扫移动端访问O(∩_∩)O

发表评论

41 + 87 =
路人甲 表情
看不清楚?点图切换 Ctrl+Enter快速提交
正在加载中……