创建数组就不再说了 array()函数
提取数组可以用list().
测试数组元素 ,可以用is_array()
接受一个参数,传进来的变量,如果变量是数组,返回true如果不是返回false.
添加和删除数组元素
数组的fifo和lifo
先解释下什么是fifo和lifo
删除元素与加入元素的顺序相同,称为先进先出,既first-in-first-out
删除元素与加入元素的顺序相反,称为后进先出,既last-in-first-out
相对应的对数组的操作
在数组头添加元素,所有已有的数值键都会相应的改变,以反映在数组中的新位置,关联键不受影响.
例:
$words = array('a','b','c');
print_r($words);
//array([0]->a,[1]->b,[2]->c);
array_unshift($words,'d');
print_r($words);
//array([0]->d,[1]->a,[2]->b,[3]->c);
从数组头删除元素,删除并返回数组中找到的元素,结果是,如果使用的是数值键,则所有相应的值都会下移.如果使用的是关联键,数组不受影响.
例:
$words = array('a','b','c');
print_r($words);
//array([0]->a,[1]->b,[2]->c);
array_shift($words,'a');
print_r($words);
//array([0]->b,[1]->c);
从数组尾添加元素,将variable添加到数组的末尾,成功返回true 失败返回false.可以传递多个参数,作为输入,同时向数组压入多个变量.
例:
<span style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #000000"><span style="FONT-FAMILY: Courier New"><span style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #0000bb"><?php<BR style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif">$stack </SPAN><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #007700">= array(</SPAN><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #dd0000">"orange"</SPAN><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #007700">, </SPAN><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #dd0000">"banana"</SPAN></SPAN><SPAN style="FONT-FAMILY: Courier New"><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #007700">);<BR style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif"></SPAN><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #0000bb">array_push</SPAN><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #007700">(</SPAN><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #0000bb">$stack</SPAN><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #007700">, </SPAN><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #dd0000">"apple"</SPAN><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #007700">, </SPAN><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #dd0000">"raspberry"</SPAN></SPAN><SPAN style="FONT-FAMILY: Courier New"><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #007700">);<BR style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif"></SPAN><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #0000bb">print_r</SPAN><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #007700">(</SPAN><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #0000bb">$stack</SPAN></SPAN><SPAN style="FONT-FAMILY: Courier New"><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #007700">);<BR style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif"></SPAN><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #0000bb">?></span></span></span>
本例将使 $stack 具有如下单元:
Array ( [0] => orange [1] => banana [2] => apple [3] => raspberry )
-------------------------------------------------------------
上例摘自PHP手册.对于数组操作,使用$array[]=,可以增加效率.因为不需要调用函数.
--------------------聪明的分割线-----------------------------
从数组末尾删除元素并返回数组的最后一个元素.
<span style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #000000"><span style="FONT-FAMILY: Courier New"><span style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #0000bb"><?php<BR style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif">$stack </SPAN><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #007700">= array(</SPAN><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #dd0000">"orange"</SPAN><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #007700">, </SPAN><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #dd0000">"banana"</SPAN><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #007700">, </SPAN><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #dd0000">"apple"</SPAN><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #007700">, </SPAN><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #dd0000">"raspberry"</SPAN></SPAN><SPAN style="FONT-FAMILY: Courier New"><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #007700">);<BR style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif"></SPAN><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #0000bb">$fruit </SPAN><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #007700">= </SPAN><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #0000bb">array_pop</SPAN><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #007700">(</SPAN><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #0000bb">$stack</SPAN></SPAN><SPAN style="FONT-FAMILY: Courier New"><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #007700">);<BR style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif"></SPAN><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #0000bb">print_r</SPAN><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #007700">(</SPAN><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #0000bb">$stack</SPAN></SPAN><SPAN style="FONT-FAMILY: Courier New"><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #007700">);<BR style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif"></SPAN><SPAN style="LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体, Verdana, Arial, Helvetica, sans-serif; COLOR: #0000bb">?></span></span></span>
经过此操作后,$stack 将只有 3 个单元:
Array ( [0] => orange [1] => banana [2] => apple )
------------------------------------------------- -----
上記の例はPHPマニュアルからの引用です
---------------------スマートな分割線---------------------
私自身の要約と意見の一部。
上記の 4 つの関数は、場合によっては非常に便利です。まず戻り値について説明します。
array_unshift() と array_push() の戻り値は Boolean 型である必要がありますが、マニュアルや関連書籍では戻り値が int 型であることが示されていることに注意してください。2 つの関数 array_shift() と array_pop() は、関連する配列の要素を削除するだけでなく、削除された配列の要素を返すこともできます。後で詳しく説明します。
http://www.bkjia.com/PHPjc/326756.html