1. 程式人生 > >PHP學習練手(三)

PHP學習練手(三)

1、建立一維陣列

  1. 方式一:一次新增一個元素構建陣列

    $band[] = ‘Jemi’;
    $band[] = 'Bret';
    
  2. 方式二:新增元素時指定鍵

    $fruits['apple'] = 3;
    $fruits['orange'] = 1;
  3. 使用array()函式

    $state = array('IA'=>'Iowa, 'MD'=>'Maryland');
    $artists = array('Shins', 'Eels');
  4. 設定第一個數字鍵值,此後新增的值將是可以遞增的鍵

    $days = array(1=>'Sun', 'Mon', 'Tue');
  5. 建立連續數字的陣列,用range()函式

    $ten = range(1,10);

    2、訪問陣列

    foreach($array as $value)
    {
       #code...
    }
    foreach($array as $key => $value)
    {
        #code ...
    }

    示例:
    calendar.php:

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Document</title>
    </head>
    <body>
    <form
    action="calendar.php" method="post">
    <?php # Script 2.6 - calendar.php //make the months array: $months =array(1=>'1月','2月','3月','4月','5月','6月','7月','8月', '9月','10月','11月','12月'); //make the days and years $days = range(1
    , 31); $years = range(2011, 2021); //make the months pull-dowm menu echo '<select name="month">'; foreach ($months as $key => $value) { echo "<option value=\"$key\"> $value</option>\n"; } echo '</select> '; //make the days pull-dowm menu echo '<select name="day">>'; foreach ($days as $value) { echo "<option value=\"$value\"> $value</option>\n"; } echo '</select> '; //make the days pull-dowm menu echo '<select name="year">>'; foreach ($years as $value) { echo "<option value=\"$value\"> $value</option>\n"; } echo '</select>'; ?>
    </form> </body> </html>

    這裡寫圖片描述

3、多維陣列

  1. 先建立一維陣列,然後再組合


    $primes = array(1, 3, 5, 7);
    $sphenic = array(30, 42, 66, 70);
    $numbers = array('Primes' => $primes, 'Sphenic' => $aphenic);

示例
multi.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <?php #Script 2.7 - mutl.php 
        $mexico = array(
                    'YU' => 'Yucatan',
                    'BC' => 'Baja California',
                    'OA' => 'Oaxaca'
            );

        $us = array(
                    'MD' => 'Maryland',
                    'IL' => 'Illiniois',
            );

        $canada = array('QC' => 'Quebec');

        $n_america = array(
                    'Mexico' => $mexico,
                    'United States' => $us,
                    'Canada' => $canada
            );

        foreach ($n_america as $country => $list) {
            echo "<h2>$country</h2>";
            echo '<ul>';
            foreach ($list as $key => $value) {
                echo "<li>$key - $value</li>";
            }
            echo '</ul>';
        }

    ?>

</body>
</html>

這裡寫圖片描述

4、陣列和字串

  • 字元=》陣列:
$array = explode(separator, $string);//以指定的分割符將字串分割為陣列

例:

$s1 = 'Mon-Tue-Wed_Thu_Fri';
$days_array = explode('-', $s1);
  • 陣列=》字元:
$string = implode(glue, $array);        //將陣列轉變成字元,字元之間以glue連線

例:

$2= implode(',', $days_array());

5、陣列排序

  • sort():按值對陣列排序,並丟棄原來的鍵,排序後重置陣列的鍵

  • ksort():按鍵對陣列排序

  • asort():按值對陣列排序

  • rsort()、arsort()、krsort():以相反的順序對陣列排序

示例:
sorting.php:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <table border="0" cellspacing="3" cellpadding="3" align="center">
        <tr>
            <td><h2>Rating</h2></td>
            <td><h2>Title</h2></td>
        </tr>

    <?php #Script 2.8 - sorting.php
    $movies = array(
        'Casablance' => 10,
        'To kill a Mockingbird' => 10,
        'The English Patient' => 2,
        'Strange Than Fiction' => 9,
        'Story of the Weeping Camel' => 5,
        'Donnie Darko' => 7
    );

    //原始順序
    echo '<tr><td colspan = "2" align = "center" >原始順序</td></tr>';
    foreach ($movies as $title => $rating) {
        echo "<tr><td>$rating</td>
                            <td>$title</td>\n";
    }

    //按鍵排序
    ksort($movies);
    echo '<tr><td colspan = "2" align = "center">按鍵排序</td></tr>';
    foreach ($movies as $title => $rating) {
        echo "<tr><td>$rating</td>
                            <td>$title</td>\n";
    }

    //按值反向排序
    arsort($movies);
    echo '<tr><td colspan = "2" align = "center">按值反向排序</td></tr>';
    foreach ($movies as $title => $rating) {
        echo "<tr><td>$rating</td>
                            <td>$title</td>\n";
    }

    ?>
    </table>
</body>
</html>

這裡寫圖片描述

補充:

  1. shuffle():隨機排序陣列
  2. natsort():更自然的順序對陣列排序