【自学php】第四天

  php支持两种数组,数字索引数组和关联数组。关联数组有点类似Map,可以用字符串或其他数据类型做键对应相应的值保存在数组中。

  1.初始化数组

  数字索引数组的初始化可以使用如下代码:

  $products = array('Tires', 'Oil, 'Spark Plugs');

  因为数组和其他变量一样,不需要预先初始化或创建,所以下面的代码创建了和上面相同的数组:

  $products[0] = 'Tires';

  $products[1] = 'Oil';

  $products[2] = 'Spark Plugs';

  关联数组的初始化:

  $prices = array('Tires' =>100, 'Oil' =>10, 'Spark Plugs' =>4);

  也可以用$prices['Tires'] = 100这样的形式来初始化。

  2.访问数组元素

  可以使用数字索引或关键字来访问数组元素:$products[0]$prices['Tires']

  使用循环访问数字索引数组:

        for($i=0; $i<3; $i++){
            echo $products[$i]." ";
        }
        foreach ($products as $current) {
            echo $current." ";
        }

  使用循环访问关联数组:

     foreach ($prices as $key => $value) {
            echo "$key - $value<br>";
        }
        /*
        遍历数组后,数组指针在尾部,想要重新遍历数组要用reset()函数把指针重置到数组头部
        */
        reset($prices);
        /*
        each()函数返回当前元素的数组,并将指针指向下一个元素。
        $element也是一个关联数组,key和0指向当前元素的关键字,value和1指向当前元素的值。
        */
        while ($element = each($prices)) {
            echo $element['key'];
            echo " -- ";
            echo $element['value'];
            echo "<br>";
        }

        reset($prices);
        /*
        list()允许命名新变量,将each()返回的数组中的0,1两个元素赋给新变量
        */
        while (list($product, $price) = each($prices)) {
            echo "$product --- $price<br>";
        }

  3.数组排序

  sort()函数可以将数组进行升序排序,它的第一个参数是数组,第二个参数是可选的:SORT_REGULA(默认值), SORT_NUMERIC,SORT_STRING。

  asort()和ksort()用于关联数组排序,asort()根据每个元素值进行排序,ksort()根据关键字排序。

  实现反向排序的对应函数是rsort(), arsort()和krsort()。

  shuffle()将数组随机排序,array_reverse()给数组反向排序。

  现在为汽车零件网店做一个动态的首页,将零件的图片存在数组中,随机排序然后选出前3张,frontpage.php如下:

<?php
    $pictures = array('tire.jpg', 'oil.jpg', 'spark_plug.jpg', 'saddle.jpg', 'steering_wheel.jpg');
    shuffle($pictures);
?>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>汽车配件网店</title>
</head>
<body>
    <h1>汽车配件网店</h1>
    <div align="center">
        <table width="100%">
            <tr>
                <?php
                    for($i=0; $i<3; $i++){
                        echo "<td align='center'><img src='".$pictures[$i]."'/></td>";
                    }
                ?>
            </tr>
        </table>
    </div>
</body>
</html>
View Code

  4.从文件载入数组

  file()函数将整个文件载入一个数组中,文件中的每一行成为数组中的一个元素。

  explode()函数用分隔符把字符串分割成数组。

  用上述两个函数将订单查看页面重新显示一下,vieworders2.php:

<?php
    $DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
?>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>订单查看页</title>
</head>
<body>
    <h1>汽车配件网店</h1>
    <h2>查看订单</h2>
    <?php
        $orders = file("$DOCUMENT_ROOT/../orders/orders.txt");
        $orderamount = count($orders);

        if($orderamount == 0){
            echo "<p><strong>暂时还没有订单,请稍后重试。</strong></p></body></html>";
            exit;
        }

        echo "<table border='1'>
";
        echo "<tr class='title'>
                <th>Order Date</th>
                <th>Tires</th>
                <th>Oil</th>
                <th>Spark Plugs</th>
                <th>Total</th>
                <th>Address</th>
              </tr>";

        foreach ($orders as $order) {
            $line = explode("	", $order);
            $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>
View Code

  显示结果如下:

    

  5.数组的其他操作

  array_push()将新元素添加到数组的末尾。

  array_pop()删除并返回数组末尾的元素。

  count()和sizeof()统计数组元素的个数。array_count_values(),统计每个元素出现的次数,它会返回一个关联数组,数组元素为键,值就是该元素出现的次数。

  还有很多其他操作数组的函数,需要的话可以查看API。

原文地址:https://www.cnblogs.com/pandabunny/p/3742717.html