PHP基礎練習-依序取出陣列中的值

有一個 9 個元素的陣列

$a = array("a","b","c","d","e","f","g","h","i","j");

我們可以用以下 4 個方法依照順序取出來

方法 1:

foreach($a as $key=>$value)
{
    echo $key." => ".$value."<br />";
}

另外一種 foreach 用法

foreach($a as $key=>$value):
    echo $key." => ".$value;
    echo "<br />";
endforeach;

方法 2:

for($i=0;$i<=9;$i++)
{
    echo $i." => ".$a[$i]."<br >";
}

方法 3:

$i = 0;
while($i<=9)
{
    echo $i." => ".$a[$i]."<br />";
    $i++;
}

方法 4:

$i = 0;
do
{
    echo $i." => ".$a[$i]."<br />";
    $i++;
}
while($i <= 9);

都可以得到以下結果
Borwser結果
0 => a
1 => b
2 => c
3 => d
4 => e
5 => f
6 => g
7 => h
8 => i
9 => j

另外可以使用以下 2 個函數來倒出陣列中的值

print_r($a);

Array ( [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f [6] => g [7] => h [8] => i [9] => j )

var_dump($a);

array(10) { [0]=> string(1) “a” [1]=> string(1) “b” [2]=> string(1) “c” [3]=> string(1) “d” [4]=> string(1) “e” [5]=> string(1) “f” [6]=> string(1) “g” [7]=> string(1) “h” [8]=> string(1) “i” [9]=> string(1) “j” }