PHP array foreach Traversal output example detailed

Source: Internet
Author: User
Tags php foreach php template

A simple php array function, before this need has not been known to have such a function, wipe the sweat ...

PHP Array Reverse output code

The code is as follows Copy Code
foreach (Array_reverse ($array) as $key => $value) {
echo $value. '
';
}


Array_reverse
(PHP 4, PHP 5)
array_reverse-returns an array of cells in reverse order

Description

Array array_reverse (array $array [, BOOL $preserve _keys])
Array_reverse () takes an array of arrays as input and returns a new array of cells in reverse order, and retains the original key name if Preserve_keys is TRUE.

Example #1 Array_reverse () example

  code is as follows copy code

<?php
$input   = Array ("PHP", 4.0, Array ("Green", "red"));
$result = Array_reverse ($input);
$result _keyed = Array_reverse ($input, TRUE);
?

This will allow the $result and $result _keyed to have the same unit, but note the difference between the key names. The printout of the $result and $result _keyed is:
Array
(
[0] => Array
(
[0] => Green
[1] => Red
)

[1] => 4
[2] => PHP
)
Array
(
[2] => Array
(
[0] => Green
[1] => Red
)

[1] => 4
[0] => PHP
)

example, in the PHP template engine

Template file:

The code is as follows Copy Code

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>{$web _tile}</title>
<body>
{$article _title}
<br/>
--by {$author}
<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>

Parsing Engine:

The code is as follows Copy Code
Var
$pattern _var = "/{$left _tag}\\$ ([\w\d]+) {$right _tag}/";
$replace _var = ' <?php echo\ $this->var_tpl_arr[' $];? > ';

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")) {
Filter label does not exist filter
$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);
}

After parsing:

The code is as follows Copy Code

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title><?php echo $this->var_tpl_arr["Web_tile"];? ></title>
<body>
<?php echo $this->var_tpl_arr["Article_title"];? >
<br/>
-by <?php Echo $this->var_tpl_arr["author"];? >
<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>

Use:

The code is as follows Copy Code

<?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 does 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.