iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 26
1
Modern Web

~網頁入門~系列 第 26

Day26 – PHP – 陣列

一維陣列

https://ithelp.ithome.com.tw/upload/images/20191012/20120959FSOw3ixmD8.jpg
陣列是儲存格的概念,從第 0 個儲存格開始,在儲存格內放入值。

用 array() 宣告陣列

  • 索引式陣列 (Indexed Array)
<?php
    $colors = array("red", "green", "blue");

    echo $colors[0]."<br>";
    echo $colors[1]."<br>";
    echo $colors[2]."<br>";
?>

https://ithelp.ithome.com.tw/upload/images/20191012/201209599OIQIKVMkK.jpg

  • 關聯式陣列 (Associative Array)
<?php
    $scores = array("math" => 85, "english" => 90, "science" => 76);

    echo $scores["math"]."<br>";
    echo $scores["english"]."<br>";
    echo $scores["science"]."<br>";
?>

https://ithelp.ithome.com.tw/upload/images/20191012/20120959g6RGJUHFl2.jpg

這兩者差別在索引的形式,內部的值皆可放數字及字串。


我們可以用 var_dump()print_r() 來看陣列的所有內容

<?php
    $colors = array("red", "green", "blue");

    var_dump($colors);  //較詳細
    echo "<br>";
    print_r($colors);   //較簡潔
    echo "<br>";
?>

https://ithelp.ithome.com.tw/upload/images/20191012/20120959xkFuB4GADo.jpg

<?php
    $scores = array("math" => 85, "english" => 90, "science" => 76);

    var_dump($scores);  //較詳細
    echo "<br>";
    print_r($scores);   //較簡潔
    echo "<br>";
?>

https://ithelp.ithome.com.tw/upload/images/20191012/20120959qWuQUh3QZB.jpg


指定索引

我們也可以直接指定索引及變數,將資料指派進去

<?php
    $colors = array("red", "green", "blue");
    $colors[3] = "yellow";
    $colors[5] = 123;

    print_r($colors);
    echo "<br>";
?>

https://ithelp.ithome.com.tw/upload/images/20191012/20120959wl004y97xW.jpg

<?php
    $scores = array("math" => 85, "english" => 90, "science" => 76);
    $scores["chinese"] = 666;
    $scores[4] = "100分";

    print_r($scores);
    echo "<br>";
?>

https://ithelp.ithome.com.tw/upload/images/20191012/20120959DL1RJAwjc5.jpg

不指定索引

若不指定索引,預設為數字編號

<?php
    $cityArray[] = "Taipei";
    $cityArray[] = "Taichung";
    $cityArray[] = "Tainan";
    
    print_r($cityArray);
?>

https://ithelp.ithome.com.tw/upload/images/20191012/20120959N6LEk5k1Rg.jpg

一維陣列範例

<?php
    $scores["math"] = 87;
    $scores["english"] = 92;
    $scores["science"] = 79;

    $subject = "english";
    echo "My ".$subject." test is ".$scores[$subject]." points.<br>";
    $subject = "math";
    echo "My ".$subject." test is ".$scores[$subject]." points.<br>";
?>

https://ithelp.ithome.com.tw/upload/images/20191012/20120959flEpPXQhOE.jpg

二維陣列

https://ithelp.ithome.com.tw/upload/images/20191012/20120959gLESKTQJxP.jpg

宣告二維陣列

<?php
    $numbers = array(array(1,2,3), array(4,5,6), array(7,8,9));

    print_r($numbers);
    echo "<br>".$numbers[1][0];
?>

https://ithelp.ithome.com.tw/upload/images/20191012/20120959i21IjpUS7r.jpg

https://ithelp.ithome.com.tw/upload/images/20191012/20120959q93iOKDMEN.jpg
有點類似 HTML 提過的表格的概念

<?php
    $data = array(
        "Elsa"=>array(
            "Math"=>78,
            "Eng"=>98
        ),
        "Anna"=>array(
            "Math"=>80,
            "Eng"=>56
        )
     );

     print_r($data);
     echo "<br>".$data["Anna"]["Math"];
?>

https://ithelp.ithome.com.tw/upload/images/20191012/20120959j5uNNP0k42.jpg

<?php
    $student["Elsa"]= array("Math"=>78,"Eng"=>98);
    $student["Anna"]= array("Math"=>80,"Eng"=>56);

    print_r($student);
    echo "<br>".$student["Anna"]["Math"];
?>

https://ithelp.ithome.com.tw/upload/images/20191012/20120959Z4aTs9GSce.jpg


上一篇
Day25 – PHP – 運算子
下一篇
Day27 – PHP – HTTP Method (Get & Post)
系列文
~網頁入門~30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
johnqq
iT邦新手 5 級 ‧ 2020-09-11 15:18:15

請問這樣也是一種陣列使用方式嗎?

<?php
$ARRAY2['NO1']="test1";

echo $ARRAY2['NO1']."<br />";;

foreach($ARRAY2 as $keys=>$values){
    echo $keys."=>".$values."<br />";
}
?>
Sherry iT邦新手 5 級 ‧ 2020-09-11 17:39:32 檢舉

你是說 foreach ($陣列 as $索引 => $值) 嗎?
這是可以一次取陣列所有值的一種方式沒錯

johnqq iT邦新手 5 級 ‧ 2020-09-11 20:48:52 檢舉

不是,是指

$ARRAY2['NO1']="test1";

在此之前我以為都要事先array("test1","test2")宣告才可以使用

$colors = array("red", "green", "blue");

原來還可以直接使用

我發文之後才發現您這篇文章最後有這個範例

 $student["Elsa"]= array("Math"=>78,"Eng"=>98);
Sherry iT邦新手 5 級 ‧ 2020-09-13 11:51:12 檢舉

喔喔~ 原來是說這個
$ARRAY2['NO1']="test1"; 這種就是可以自己指定索引名稱
也可以不指定[]內容,預設就是數字索引
當然也可以用array()來宣告

我要留言

立即登入留言