Home > Backend Development > PHP Tutorial > php foreach output in forward and reverse order

php foreach output in forward and reverse order

巴扎黑
Release: 2016-11-12 11:17:29
Original
1582 people have browsed it

// 正序
foreach($files as $file_num => $file) {
if(is_file($directory.$file)){
//$file = iconv("gb2312","UTF-8",$file);  //或者 iconv("gb2312","UTF-8",$value);
$date = substr($file,0,9);
echo &#39;<li class="world-cup-&#39;.$date.&#39;">&#39;;
echo &#39;<a href="&#39;.$directory.$file.&#39;" rel="worldcup" title="巴西世界杯赛事&#39;.$date.&#39;">&#39;;
echo &#39;<img src="&#39;.$directory.$file.&#39;" alt="">&#39;;
echo &#39;</a>&#39;;
echo &#39;</li>&#39;;
}
}
// 倒序
foreach(array_reverse($files) as $file_num => $file) {
if(is_file($directory.$file)){
//$file = iconv("gb2312","UTF-8",$file);  //或者 iconv("gb2312","UTF-8",$value);
$date = substr($file,0,9);
echo &#39;<li class="world-cup-&#39;.$date.&#39;">&#39;;
echo &#39;<a href="&#39;.$directory.$file.&#39;" rel="worldcup" title="巴西世界杯赛事&#39;.$date.&#39;">&#39;;
echo &#39;<img src="&#39;.$directory.$file.&#39;" alt="">&#39;;
echo &#39;</a>&#39;;
echo &#39;</li>&#39;;
}
}
Copy after login

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template