首先在服务端存有文件orders.txt
将文件载入数组并显示,创建vieworder2.php
<?php $DOCUMENT_ROOT =$_SERVER['DOCUMENT_ROOT'];?><!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>客户订单</title> <style> table,tr,th,td{ border-collapse: collapse; border: 1px solid #ccc; padding: 5px; } th{ background-color: #ccccff; } td{ text-align: right; } </style></head><body><h1>我们的商店</h1><h2>客户订单</h2><?php //将文件载入 $orders=file("$DOCUMENT_ROOT/L03/files/orders.txt"); //统计元素个数 $number_of_orders=count($orders); //错误信息提示 if($number_of_orders==0){ echo "<p><strong>没有订单信息,请再试一试!</strong></p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/7fc7563c4182" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">PHP免费学习笔记(深入)</a>”;</p>"; } echo "<table> <tr> <th>订购日期</th><th>男装</th><th>鞋子</th><th>眼镜</th><th>总计</th><th>收货地址</th> </tr>"; for($i=0;$i<$number_of_orders;$i++){ //按空格对元素进行分割 $line=explode("\t",$orders[$i]); //将一个字符串转换为一个整数 $line[1]=intval($line[1]); $line[2]=intval($line[2]); $line[3]=intval($line[3]); echo "<tr> <td>$line[0]</td><td>$line[1]</td><td>$line[2]</td><td>$line[3]</td><td>$line[4]</td><td>$line[5]</td> </tr>"; } echo "</table>";?></body></html>
补充知识点:
<?php//创建一个数组$products=array('cloths','shoes','glasses');//创建一个1~10的升序数组$numbers=range(1,10);//创建一个1~10的奇数数组$odds=range(1,10,2);//创建一个a~z的字母数组$letters=range('a','z');//改变数组元素的内容$products[0]='spinner';//增加一个新元素到数组末尾$products[3]='skit';//显示数组内容echo "$products[0] $products[1] $products[2] $products[3]";//使用循环访问数组echo '<br/>';for($i=0;$i<26;$i++){//使用for循环 echo $letters[$i]." ";}echo '<br/>';foreach($odds as $current){//使用foreach echo $current." ";}//初始化关联数组$prices=array('cloths'=>150,'shoes'=>300,'glasses'=>35);//使用循环语句访问关联数组的3种方法echo '<br/>';foreach($prices as $key=>$value){//使用foreach echo $key.'_'.$value.'<br/>';}reset($prices);//使用reset函数将当前元素重新设置到数组开始处while($element=each($prices)){//使用each函数 echo $element['key'].'_'.$element['value'].'<br/>';}reset($prices);while(list($product,$price)=each($prices)){//使用list函数 echo "$product - $price<br/>";}//创建一个二维数组$products2=array( array('CLOTH','cloths',150), array('SHOE','shoes',300), array('GLASS','glasses',35));//使用双重for来访问数组元素for($row=0;$row<3;$row++){ for($column=0;$column<3;$column++){ echo ' | '.$products2[$row][$column]; } echo ' |<br/>';}//创建一个二维关联数组$products3=array(array('Code'=>'CLOTH','Description'=>'cloths','Price'=>150), array('Code'=>'SHOE','Description'=>'shoes','Price'=>300), array('Code'=>'GLASS','Description'=>'glasses','Price'=>35));//使用for循环访问二维关联数组for($row=0;$row<3;$row++){ echo '|'.$products3[$row]['Code'].'|'.$products3[$row]['Description'].'|'.$products3[$row]['Price'].'|<br/>';}//使用each函数和list函数来访问reset($products3);for($row=0;$row<3;$row++){ while(list($key,$value)=each($products3[$row])){ echo "|$value"; } echo '|<br/>';}/************数组排序*****************///使用sort函数按字母升序进行排序$products4=array('cloths','shoes','glasses');sort($products4);//使用sort函数按数字升序进行排序$prices3=array(150,300,35);sort($prices3);//使用asort函数根据数组的每个元素值进行排序$prices4=array('cloths'=>150,'shoes'=>300,'glasses'=>35);asort($prices4);//使用ksort函数根据数组的关键字进行排序ksort($prices4);//另外对应的反向排序方法有rsort()、arsort()、krsort();//用户定义排序:usort()$products5=array( array('CLOTH','cloths',150),array('SHOE','shoes',300), array('GLASS','glasses',35));function compare($x,$y){ if($x[1]==$y[1]){ return 0; }else if($x[1]<$y[1]){ return -1; }else{ return 1; }}usort($products5,'compare');//对数组进行重新排序:随机排序shuffle()、反向排序array_reverse()$numbers=range(1,10);//$numbers=array_reverse($numbers);$numbers=shuffle($numbers);for($i=0;$i<3;$i++){//使用for循环 echo $numbers[$i]." ";}数组常用函数补充
current($array_name)??返回第一个元素;
next($array_name)??将指针前移,然后再返回新的当前元素;
each($array_name)??在指针前移一个位置之前返回当前元素;
reset($array_name)??将返回指向数组第一个元素的指针;
end($array_name)??将指针移到数组末尾;
prev($array_name)??将当前指针往回移再返回新的当前元素,与next()相反;
array_walk()??对数组的每一个元素应用任何函数;
count()、sizeof()、array_count_values()??统计数组元素个数;
extract()??通过一个数组创建一系列的标量变量;
更多数组的知识点请查看php手册_数组
版权声明:本文为博主原创文章,未经博主允许不得转载。
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号