簡單的一個php數組函數,之前沒這個需要一直都不知道有這麼一個函數,擦汗...
php數組逆序輸出代碼
代碼如下 復制代碼 foreach(array_reverse($array) AS $key=>$value){
array_reverse
(PHP 4, PHP 5)
array_reverse — 返回一個單元順序相反的數組
說明
array array_reverse ( array $array [, bool $preserve_keys ] )
array_reverse() 接受數組 array 作為輸入並返回一個單元為相反順序的新數組,如果 preserve_keys 為 TRUE 則保留原來的鍵名。
Example #1 array_reverse() 例子
代碼如下 復制代碼<?php
$input = array("php", 4.0, array("green", "red"));
$result = array_reverse($input);
$result_keyed = array_reverse($input, TRUE);
?>
這將使 $result 和 $result_keyed 具有相同的單元,但是注意鍵名的區別。$result 和 $result_keyed 的打印輸出顯示分別為:
Array
(
[0] => Array
(
[0] => green
[1] => red
)
[1] => 4
[2] => php
)
Array
(
[2] => Array
(
[0] => green
[1] => red
)
[1] => 4
[0] => php
)
例子,在PHP模板引擎中
模板文件:
代碼如下 復制代碼<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>{$web_tile}</title>
</head>
<body>
{$article_title}
<br/>
-- by {$author}
<hr/>
<br/>
{$content}
<br/>
-- publish @ {$time}
<br/>
<br/>
foreach test:
{foreach ( from=url key=b item=c )}
<a href="index.php?artcle_id={==b}">{==c}</a>
{/foreach}
<br/>
</body>
</html>
解析引擎:
代碼如下 復制代碼 // var解析後:
代碼如下 復制代碼<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><?php echo $this->var_tpl_arr["web_tile"];?></title>
</head>
<body>
<?php echo $this->var_tpl_arr["article_title"];?>
<br/>
-- by <?php echo $this->var_tpl_arr["author"];?>
<hr/>
<br/>
<?php echo $this->var_tpl_arr["content"];?>
<br/>
-- publish @ <?php echo $this->var_tpl_arr["time"];?>
<br/>
<br/>
foreach test:
<?php foreach($this->var_tpl_arr["url"] as $b => $c) { ?>
<a href="index.php?artcle_id=<?php echo $b;?>"><?php echo $c;?></a>
<?php } ?>
<br/>
</body>
</html>
使用:
代碼如下 復制代碼<?php
require_once 'core/YATP.class.php';
$app = new YATP();
date_default_timezone_set("Asia/Shanghai");
$app->is_cache = false;
$article_title = "yet,it is a simple template engine";
$author = "[email protected]";
$web_tile = "just test ";
$content = "It is easy to write a simple template engine for yourself,what u can do is try to do it!";
$time = date("Y-m-d H:i:s",time());
$url = array(
"url1"=>"http://www.111cn.net",
"url2"=>"http://www.111cn.net",
);
$app->assign("article_title",$article_title);
$app->assign("author",$author);
$app->assign("web_tile",$web_tile);
$app->assign("content",$content);
$app->assign("time",$time);
$app->assign("url",$url);
$app->display("index.html");
// end of script