php数组增加元素
截图
代码
1 <HTML> 2 <HEAD> 3 <TITLE>给数组增加元素</TITLE> 4 </HEAD> 5 <? 6 $Cities[] = "<B>北京</B>"; //等同于$Cities[0] = "北京" 7 $Cities[] = "<B>天津</B>"; //等同于$Cities[1] = "天津" 8 $Cities[] = "<B>上海</B>"; //等同于$Cities[2] = "上海" 9 $Cities[] = "<B>深圳</B>"; //等同于$Cities[3] = "深圳" 10 /* 11 ** 统计元素个数 12 */ 13 $indexLimit = count($Cities); //把数组中元素的个数赋给$indexLimit 14 /* 15 ** 打印所有数组 16 */ 17 for($index=0; $index < $indexLimit; $index++) 18 { 19 print("第 $index 个城市是 $Cities[$index]。 <BR> "); 20 } 21 ?> 22 </BODY> 23 </HTML>
count用于统计数组个数
数组的初始化用的是 array(1=>"January", "February", "March",//初始化一个数组
用的也是括号
1 <?php 2 $sites = array 3 ( 4 "runoob"=>array 5 ( 6 "菜鸟教程", 7 "http://www.runoob.com" 8 ), 9 "google"=>array 10 ( 11 "Google 搜索", 12 "http://www.google.com" 13 ), 14 "taobao"=>array 15 ( 16 "淘宝", 17 "http://www.taobao.com" 18 ) 19 ); 20 print("<pre>"); // 格式化输出数组 21 print_r($sites); 22 print("</pre>"); 23 ?>