php中如何初始化一個數組

來源:互聯網
上載者:User
數組

初始化一個數組一般有2種方法:一個是單獨給數組中的元素賦值;還有一種是把所有的元素一起初始化。
下面簡要介紹初始化數組的2種方法:
第一種方法:
$a["color"]="red";
$a["taste"]="sweet";
$a["shape"]="round";
$a["name"]="apple";
$a[3]=4;
第二種方法:
$a=array(
"color"=>"red",
"taste"=>"sweet",
"shape"=>"round",
"name"=>"apple",
3=>4);

這兩種方法的效果是相同的,不同的只是給元素賦值的方法不同。

下面是一個例子,需要注意的是,數組中元素的賦值是從第二個元素開始的。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>初始化數組</title>
</head>

<body>
<font size="5">
<?php
$monthName=array(1=>"January","February","March","April","May","June","July","August","September","October","November","December");
print("英語的“5月”是<b>$monthName[5]</b>.<br>\n");
?>
</font>
</body>
</html>


 



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.