[PHP] list- 把陣列中的值轉換為變數

標籤: php

php 的 list 函示可以把陣列中的值轉換為變數。

語法:
list(var1,var2...)

其中至少要填第一個的值,第二個之後則是看要放多少變數去取得陣列內的值。

範例 1:

$my_array = array("Dog","Cat","Horse");

list($a​​, $b, $c) = $my_array;
echo "I have several animals, a $a, a $b and a $c.";
//輸出 I have several animals, a Dog, a Cat and a Horse.

 

範例 2:只用了 a 和 c 的變數

$my_array = array("Dog","Cat","Horse");

list($a​​, , $c) = $my_array;
echo "Here I only use the $a and $c variables.";
//輸出 Here I only use the Dog and Horse variables.