使用 PHP 建立陣列有以下幾種方法:
以數字為索引值 | <?php $name = array("Chen", "Lin", "Jang", "Lee", "Wang"); ?> |
<?php $name[0] = "Chen"; $name[1] = "Lin"; $name[2] = "Jang"; $name[3] = "Lee"; $name[4] = "Wang"; ?> | |
以字串為索引值 | <?php $name = array( "a" => "Chen", "b" => "Lin", "c" => "Jang", "d" => "Lee", "e" => "Wang" ); ?> |
<?php $name["a"] = "Chen"; $name["b"] = "Lin"; $name["c"] = "Jang"; $name["d"] = "Lee"; $name["e"] = "Wang"; ?> |
至於要如何從陣列中取出資料呢?除了使用索引值外,PHP 還提供了多種簡單方便的函式,例如 list():
// 只有在以數字為索引值時才能使用 list() $arr = array('one', 'two', 'three'); list($a, $b, $c) = $arr; echo "$a plus $b equals $c";