代码改变世界

第三章 使用数组(4)-从文件载入数组

2016-07-22 16:25  yojiaku  阅读(174)  评论(0编辑  收藏  举报
  • 从文件载入数组

 

<?php
//create a short variable name
$DOC = $_SERVER['DOCUMENT_ROOT'];
$orders = file("$DOC/../orders.txt");
$number_of_orders = count($orders);
if($number_of_orders == 0){
    echo "<p><strong>No Orders Pending.
              Please try again later.</strong></p>";
}

for($i = 0;$i < $number_of_orders;$i++){
    echo $orders[$i]."<br />";
}

程序清单3-2 vieworders.php——使用PHP显示Bob的订单内容

explain:这个脚本使用了file()函数将整个文件载入一个数组中。文件中的每行则成为数组中的一个元素。还用了count()函数来统计数组中的元素个数。

本例结果如图:

此外,还可以将订单行中的每个区段载入到单独的数组元素中,从而可以分开处理每个区段或将它们更好地格式化。

<?php
//create a short variable name
$DOC = $_SERVER['DOCUMENT_ROOT'];
?>
<html>
<head>
    <title>BOb's Auto Parts - Customer Orders</title>
</head>
<body>
    <h1>Bob's Auto Parts</h1>
    <h2>Customer Orders</h2>
    <?php
    //read in the entire file
    //each order become an element in the array
    $orders = file("$DOC/../orders.txt");

    //count the number if orders in the array
    $number_of_orders = count($orders);

    if($number_of_orders == 0){
        echo "<p><strong>No Orders Pending.
              Please try again later.</strong></p>";
    }

    echo "<table border='1'>\n";
    echo "<tr><th bgcolor='#ccccff'>Order Date</th> 
               <th bgcolor='#ccccff'>Tires</th>
               <th bgcolor='#ccccff'>Oil</th>
               <th bgcolor='#ccccff'>Spark Plugs</th>
               <th bgcolor='#ccccff'>Total</th>
               <th bgcolor='#ccccff'>Address</th>
            </tr>";

    for($i = 0;$i < $number_of_orders;$i++){
        //split up each line
        $line = explode("\t",$orders[$i]);

        //keep only the number of items ordered
        $line[1] = intval($line[1]);
        $line[2] = intval($line[2]);
        $line[3] = intval($line[3]);

        //output each order
        echo "<tr>
                <td>".$line[0]."</td>
                <td align='right'>".$line[1]."</td>
                <td align='right'>".$line[2]."</td>
                <td align='right'>".$line[3]."</td>
                <td align='right'>".$line[4]."</td>
                <td>".$line[5]."</td>
               </tr>";
    }

    echo "</table>";
    ?>
</body>
</html>

程序清单3-3 vieworders.php —— 用PHP分离、格式化并显示Bob的订单内容

explain:这个脚本将整个文件载入数组中,用了explode()函数来分割每行,这样在开始打印前就能再做一些处理与格式化。

本例输出结果:

解释代码:

for($i = 0;$i < $number_of_orders;$i++){
        //split up each line
        $line = explode("\t",$orders[$i]);

        //keep only the number of items ordered
        $line[1] = intval($line[1]);
        $line[2] = intval($line[2]);
        $line[3] = intval($line[3]);

        //output each order
        echo "<tr>
                <td>".$line[0]."</td>
                <td align='right'>".$line[1]."</td>
                <td align='right'>".$line[2]."</td>
                <td align='right'>".$line[3]."</td>
                <td align='right'>".$line[4]."</td>
                <td>".$line[5]."</td>
               </tr>";
    }

  这里的explode用"\t"为分界点,将$orders数组分割,即将订单

  分割成6个部分:Order Date, Tires, Oil, Spark Plugs, Total, Address(即:”09:3, 19th July',"1 tires","1 Oil","1 spark plugs","$114.00","One two three Street"),然后再将每个部分放入不同的表格单元以便美化输出效果。

  explode()函数原型:array explode(string separator, string string [, int limit]),其中limit可以用来限制被返回的最大块数。

  这里还用了intval()函数从字符串中提取数字。intval可以将一个字符串转化为一个整数,这个转化可以忽略掉某一部分。