PHP数组foreach遍历输出例子详解

通常我们对于数据遍历会使用到foreach来操作当然也有使用到while list each函数来实现了,但在方便面上来看foreach更简洁好用性能也非常的不错,下面本人整理了一款在开发应用中foreach前后使用例子,希望对大家会有所帮助.

简单的一个php数组函数,之前没这个需要一直都不知道有这么一个函数,擦汗...

php数组逆序输出代码

  1. foreach(array_reverse($array) AS $key=>$value){
  2. echo $value.'
  3. ';
  4. }

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() 例子,代码如下:

  1. <?php
  2. $input = array("php", 4.0, array("green", "red"));
  3. $result = array_reverse($input);
  4. $result_keyed = array_reverse($input, TRUE);
  5. ?>

这将使 $result 和 $result_keyed 具有相同的单元,但是注意键名的区别,$result 和 $result_keyed 的打印输出显示分别为:

  1. Array
  2. (
  3. [0] => Array
  4. (
  5. [0] => green
  6. [1] => red
  7. )
  8. [1] => 4
  9. [2] => php
  10. )
  11. Array
  12. (
  13. [2] => Array
  14. (
  15. [0] => green
  16. [1] => red
  17. )
  18. [1] => 4
  19. [0] => php
  20. )

例子,在PHP模板引擎中,模板文件:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>{$web_tile}</title> //开源软件:phpfensi.com
  6. </head>
  7. <body>
  8. {$article_title}
  9. <br/>
  10. -- by {$author}
  11. <hr/>
  12. <br/>
  13. {$content}
  14. <br/>
  15. -- publish @ {$time}
  16. <br/>
  17. <br/>
  18. foreach test:
  19. {foreach ( from=url key=b item=c )}
  20. <a href="index.php?artcle_>{==c}</a>
  21. {/foreach}
  22. <br/>
  23. </body>
  24. </html>

解析引擎,代码如下:

  1. // var
  2. $pattern_var = "/{$left_tag}\\$([\w\d]+){$right_tag}/";
  3. $replace_var = '<?php echo \$this->var_tpl_arr["$1"];?>';
  4. if (preg_match($pattern_var, $content)) {
  5. $content = preg_replace($pattern_var, $replace_var, $content);
  6. }
  7. // foreach
  8. preg_match_all("/{$left_tag}foreach\s+([^{]+?){$right_tag}/is", $content, $match_foreach);
  9. if (isset($match_foreach[1]) && is_array($match_foreach)) {
  10. foreach($match_foreach[1] as $match_key => $match_value) {
  11. $split_foreachs = array_filter(preg_split('/\s+/is', $match_value));
  12. $new_foreach_tag = array();
  13. foreach($split_foreachs as $split_foreach) {
  14. $split = explode("=", $split_foreach);
  15. if (count($split == 2)) {
  16. if(in_array($split[0], array("from","item","key"))) {
  17. //过滤标签 不存在过滤
  18. $new_foreach_tag[$split[0]] = $split[1];
  19. }
  20. }
  21. }
  22. $from = $key = $item = '';
  23. extract($new_foreach_tag);
  24. $key = ($key) ? '$'.$key.' =>' : '' ;
  25. $replace_foreach = '<?php foreach($this->var_tpl_arr["'.$from.'"] as '.$key.' $'.$item.') { ?>';
  26. $content = str_replace($match_foreach[0][$match_key], $replace_foreach, $content);
  27. }
  28. }
  29. $pattern_foreach = "/{$left_tag}\/foreach{$right_tag}/";
  30. $replace_foreach = "<?php } ?>";
  31. if (preg_match($pattern_foreach, $content)) {
  32. $content = preg_replace($pattern_foreach, $replace_foreach, $content);
  33. }
  34. // var in statement
  35. $pattern_var = "/{$left_tag}==([\w\d]+){$right_tag}/";
  36. $replace_var = '<?php echo \$$1;?>';
  37. if (preg_match($pattern_var, $content)) {
  38. $content = preg_replace($pattern_var, $replace_var, $content);
  39. }

解析后代码如下:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title><?php echo $this->var_tpl_arr["web_tile"];?></title>
  6. </head>
  7. <body>
  8. <?php echo $this->var_tpl_arr["article_title"];?>
  9. <br/>
  10. -- by <?php echo $this->var_tpl_arr["author"];?>
  11. <hr/>
  12. <br/>
  13. <?php echo $this->var_tpl_arr["content"];?>
  14. <br/>
  15. -- publish @ <?php echo $this->var_tpl_arr["time"];?>
  16. <br/>
  17. <br/>
  18. foreach test:
  19. <?php foreach($this->var_tpl_arr["url"] as $b => $c) { ?>
  20. <a href="index.php?artcle_><?php echo $c;?></a>
  21. <?php } ?>
  22. <br/>
  23. </body>
  24. </html>

使用代码如下:

  1. <?php
  2. require_once 'core/YATP.class.php';
  3. $app = new YATP();
  4. date_default_timezone_set("Asia/Shanghai");
  5. $app->is_cache = false;
  6. $article_title = "yet,it is a simple template engine";
  7. $author = "sanwhiteyu@tencent.com";
  8. $web_tile = "just test ";
  9. $content = "It is easy to write a simple template engine for yourself,what u can do is try to do it!";
  10. $time = date("Y-m-d H:i:s",time());
  11. $url = array(
  12. "url1"=>"http://www.phpfensi.com",
  13. "url2"=>"http://www.phpfensi.com",
  14. );
  15. $app->assign("article_title",$article_title);
  16. $app->assign("author",$author);
  17. $app->assign("web_tile",$web_tile);
  18. $app->assign("content",$content);
  19. $app->assign("time",$time);
  20. $app->assign("url",$url);
  21. $app->display("index.html");
  22. // end of script
  23. ?>