PHP数组foreach遍历输出例子详解
简单的一个php数组函数,之前没这个需要一直都不知道有这么一个函数,擦汗...
php数组逆序输出代码
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2024')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2024>foreach(array_reverse($array) AS $key=>$value){echo $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() 例子
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5398')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5398><?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模板引擎中
模板文件:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4856')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4856><!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>
解析引擎:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy8460')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy8460>// var$pattern_var = "/{$left_tag}\\$([\w\d] ){$right_tag}/";
$replace_var = '<?php echo \$this->var_tpl_arr["$1"];?>';
if (preg_match($pattern_var, $content)) {
$content = preg_replace($pattern_var, $replace_var, $content);
}
// foreach
preg_match_all("/{$left_tag}foreach\s ([^{] ?){$right_tag}/is", $content, $match_foreach);
if (isset($match_foreach[1]) && is_array($match_foreach)) {
foreach($match_foreach[1] as $match_key => $match_value) {
$split_foreachs = array_filter(preg_split('/\s /is', $match_value));
$new_foreach_tag = array();
foreach($split_foreachs as $split_foreach) {
$split = explode("=", $split_foreach);
if (count($split == 2)) {
if(in_array($split[0], array("from","item","key"))) {
//过滤标签 不存在过滤
$new_foreach_tag[$split[0]] = $split[1];
}
}
}
$from = $key = $item = '';
extract($new_foreach_tag);
$key = ($key) ? '$'.$key.' =>' : '' ;
$replace_foreach = '<?php foreach($this->var_tpl_arr["'.$from.'"] as '.$key.' $'.$item.') { ?>';
$content = str_replace($match_foreach[0][$match_key], $replace_foreach, $content);
}
}
$pattern_foreach = "/{$left_tag}\/foreach{$right_tag}/";
$replace_foreach = "<?php } ?>";
if (preg_match($pattern_foreach, $content)) {
$content = preg_replace($pattern_foreach, $replace_foreach, $content);
}
// var in statement
$pattern_var = "/{$left_tag}==([\w\d] ){$right_tag}/";
$replace_var = '<?php echo \$$1;?>';
if (preg_match($pattern_var, $content)) {
$content = preg_replace($pattern_var, $replace_var, $content);
}
解析后:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy3746')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3746><!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>
使用:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5749')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5749><?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 = "sanwhiteyu@tencent.com";
$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.jiaochengji.com",
"url2"=>"http://www.jiaochengji.com",
);
$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
您可能感兴趣的文章:
php遍历数组之list foreach each用法总结
php遍历数组的几种方法(for foreach list each while)
php数组遍历方法详解(for foreach list each key)
php数组遍历foreach ($arr as &$value)用法介绍
php foreach 循环遍历数组方法
php遍历数组list foreach each方法实例
php遍历数组 foreach each() list()方法总结
php一维数组遍历方法的比较分析
php数组循环输出实现方法
php数组入门教程之数组遍历