iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 29
1
Modern Web

~網頁入門~系列 第 29

Day29 – PHP – 陣列與迴圈的應用

count

count 用來計算集合的總數,所以可以用來計算陣列的長度。

在拿取存放在陣列的資料時,經常會使用迴圈來取值,而 count 這個屬性讓陣列的取值更加簡潔明瞭。
https://ithelp.ithome.com.tw/upload/images/20191015/20120959oGaBVviXqk.jpg

count($array) 設定為數字範圍,讓變數從 0 開始,小於陣列長度,就能拿取陣列中所有資料了。
https://ithelp.ithome.com.tw/upload/images/20191015/20120959rRywiu6ece.jpg

<?php
    $colors = array("red","blue","green");
    for ($i = 0; $i < count($colors); $i++) {
        echo $colors[$i]."<br>";
    }    
?>

https://ithelp.ithome.com.tw/upload/images/20191015/20120959JDinmCs4KG.jpg


foreach

foreach 是簡潔快速的 for 迴圈概念,將陣列中的每一個資料取出來。

  1. 取索引式陣列資料
    https://ithelp.ithome.com.tw/upload/images/20191015/20120959ixeYEmkDF4.jpg
<?php
    $colors = array("red","green","blue");
    foreach($colors as $rgb){
       echo $rgb."<br>";
    }       
?>

https://ithelp.ithome.com.tw/upload/images/20191015/201209594mlg36XlVz.jpg

  1. 取關聯式陣列資料
    https://ithelp.ithome.com.tw/upload/images/20191015/201209592yoq0zIaLK.jpg
<?php
    $subjects = array(
        "chinese" => 87,
        "english" => 63,
        "math" => 92,
    );

    foreach($subjects as $subject => $score){
       echo $subject." : ".$score."<br>";
    }       
?>

https://ithelp.ithome.com.tw/upload/images/20191015/20120959TDz72NiIRo.jpg


陣列方法

array_key_exists

https://ithelp.ithome.com.tw/upload/images/20191015/201209595NeSlZeXdD.jpg

<?php
    $subjects = array(
        "chinese" => 87,
        "english" => 63,
        "math" => 92,
    );

    var_dump(array_key_exists("math", $subjects));
    echo "<br>";
    var_dump(array_key_exists("science", $subjects));
?>

https://ithelp.ithome.com.tw/upload/images/20191015/20120959EtwFMUm7KX.jpg

array_key_exists應用

<?php
    $subjects = array(
        "chinese" => 87,
        "english" => 63,
        "math" => 92,
    );

    $input = "chinese";

    if(array_key_exists($input , $subjects) == true){
        echo $input." : ".$subjects[$input]."分<br>";
    }else{
        echo "查無此科目!";
    }
?>

https://ithelp.ithome.com.tw/upload/images/20191015/20120959LocVEn9498.jpg

如果是沒有在索引內的

$input = "science";

https://ithelp.ithome.com.tw/upload/images/20191015/20120959uT4ia8U68T.jpg

in_array

https://ithelp.ithome.com.tw/upload/images/20191015/20120959IGqC35coO8.jpg

<?php
    $subjects = array(
        "chinese" => 87,
        "english" => 63,
        "math" => 92,
    );

    var_dump(in_array("87", $subjects));
    echo "<br>";
    var_dump(in_array("100", $subjects));
?>

https://ithelp.ithome.com.tw/upload/images/20191015/20120959FUjUTzj1QY.jpg

in_array應用

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

    $color = "red";
    if(in_array($color, $colors) == true){
        echo "有 ".$color." 喔!";
    } else {
        echo "沒有這個顏色喔~";
    }
?>

https://ithelp.ithome.com.tw/upload/images/20191015/20120959FZrTVeYyf5.jpg

$color = "yellow";

https://ithelp.ithome.com.tw/upload/images/20191015/20120959idkcKBTvbW.jpg

array_search

https://ithelp.ithome.com.tw/upload/images/20191015/201209593oGxxRl7is.jpg

<?php
    $subjects = array(
        "chinese" => 87,
        "english" => 63,
        "math" => 92,
    );

    var_dump(array_search("100", $subjects));
    echo "<br>";
    var_dump(array_search("87", $subjects));
    echo "<br>";
    $subject = array_search("87", $subjects);
    echo $subject;
?>

https://ithelp.ithome.com.tw/upload/images/20191015/201209598GFdAj9C6U.jpg

array_search應用

https://ithelp.ithome.com.tw/upload/images/20191015/20120959pGObwdUgWk.jpg

https://ithelp.ithome.com.tw/upload/images/20191015/20120959jzfM9BHwwL.jpg

https://ithelp.ithome.com.tw/upload/images/20191015/20120959jFCKOQbt0R.jpg

https://ithelp.ithome.com.tw/upload/images/20191015/201209598VhZPjTCDe.jpg

https://ithelp.ithome.com.tw/upload/images/20191015/20120959i8xrkDGJDI.jpg

https://ithelp.ithome.com.tw/upload/images/20191015/20120959lmGXjajeYf.jpg


上一篇
Day28 – PHP – 流程控制
下一篇
Day30 – PHP – 表單實作
系列文
~網頁入門~30

尚未有邦友留言

立即登入留言