数组中的函数库是我们学习数组的要点,我们这里主要介绍PHP数组函数库array建立数组,和大家一起来分享一下吧。好在PHP并不难,这些东西也不多,再学几课我们就可以开始利用它来实现一个一个网站功能了,到时候相信你会越来越有精神了。好,开始今天的课程。今天我们学习数组,数组是一种数据类型,它的使用频率相当高,学会处理数组会让你做网站时得心应手。
PHP数组函数库新建数组使用array
<ol class="dp-xml"><li class="alt"><span><span>array array ( [mixed ...] ) </span></span></li></ol>
返回根据参数建立的数组。参数可以用 => 运算符给出索引。关于数组是什么的信息请阅读数组一节。注: array() 是一个语言结构,用于字面上表示数组,不是常规的函数。语法“index => values”,用逗号分开,定义了索引和值。索引可以是字符串或数字。如果省略了索引,会自动产生从 0 开始的整数索引。如果索引是整数,则下一个产生的索引将是目前最大的整数索引 + 1。注意如果定义了两个完全一样的索引,则后面一个会覆盖前一个。在最后一个定义的数组项目之后加一个逗号虽然不常见,却是合法的语法。
下面的例子演示了怎样建立一个二维数组,怎样给相应的数组指定键名,以及怎样在普通数组中略过和继续数字索引。
PHP数组函数库例子1. array() 例子
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">fruits</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array</font></span><span> ( </span> </li> <li class="alt"> <span>"fruits" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> array("a" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> "orange", "b" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> "banana", "c" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> "apple"), </span> </li> <li class=""> <span>"numbers" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> array(1, 2, 3, 4, 5, 6), </span> </li> <li class="alt"> <span>"holes" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> array("first", </span><span class="attribute"><font color="#ff0000">5</font></span><span> =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> "second", "third") </span> </li> <li class=""><span>); </span></li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
PHP数组函数库例子2. array() 的自动索引
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">array</font></span><span class="attribute-value"><font color="#0000ff">array</font></span><span> = array(1, 1, 1, 1, 1, </span><span class="attribute"><font color="#ff0000">8</font></span><span> =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 1, </span><span class="attribute"><font color="#ff0000">4</font></span><span> =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 1, 19, </span><span class="attribute"><font color="#ff0000">3</font></span><span> =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 13); </span> </li> <li class="alt"><span>print_r($array); </span></li> <li class=""> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
上例将输出:
<ol class="dp-xml"> <li class="alt"><span><span>Array </span></span></li> <li class=""><span>( </span></li> <li class="alt"> <span>[0] =</span><span class="tag">></span><span> 1 </span> </li> <li class=""> <span>[1] =</span><span class="tag">></span><span> 1 </span> </li> <li class="alt"> <span>[2] =</span><span class="tag">></span><span> 1 </span> </li> <li class=""> <span>[3] =</span><span class="tag">></span><span> 13 </span> </li> <li class="alt"> <span>[4] =</span><span class="tag">></span><span> 1 </span> </li> <li class=""> <span>[8] =</span><span class="tag">></span><span> 1 </span> </li> <li class="alt"> <span>[9] =</span><span class="tag">></span><span> 19 </span> </li> <li class=""><span>) </span></li> </ol>
注意索引 3 被定义了两次,保留了最后的值 13。索引 4 在 索引 8 之后定义,下一个自动生成的索引(值为 19 那个)为 9,因为最大的索引是 8。