>  기사  >  백엔드 개발  >  php笔记之:php数组相关函数的使用_PHP教程

php笔记之:php数组相关函数的使用_PHP教程

WBOY
WBOY원래의
2016-07-21 15:12:18862검색

创建数组就不再说了  array()函数

提取数组可以用list().

测试数组元素 ,可以用is_array()

接受一个参数,传进来的变量,如果变量是数组,返回true如果不是返回false.

添加和删除数组元素

数组的fifo和lifo

先解释下什么是fifo和lifo

删除元素与加入元素的顺序相同,称为先进先出,既first-in-first-out

删除元素与加入元素的顺序相反,称为后进先出,既last-in-first-out

相对应的对数组的操作

  • array_unshift()
  • int aray_unshift(array array,mixed variable [,mixed variable...])

在数组头添加元素,所有已有的数值键都会相应的改变,以反映在数组中的新位置,关联键不受影响.

例:

$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);

  • array_shift()
  • mixed array_shift(array array)

从数组头删除元素,删除并返回数组中找到的元素,结果是,如果使用的是数值键,则所有相应的值都会下移.如果使用的是关联键,数组不受影响.

例:

$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);

  • array_push()
  • int array_push(array array ,mixed variable [,mixed variable...])

从数组尾添加元素,将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[]=,可以增加效率.因为不需要调用函数.

--------------------聪明的分割线-----------------------------

  • array_pop()
  • mixed array_pop(array target_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手册

-------------------------聪明的分割线-----------------

自己的一些总结和观点.

以上四个函数,有的时候可能用处很大,首先说返回值

注意array_unshift()和array_push()的返回值应该是布尔型,但是手册上和相关图书上都标注说返回的是int型.这个值得思考.

array_shift()和array_pop()两个函数不仅能够实现删除相关数组的元素,并且能够返回所删除的数组元素.先写到这里,后面还有很多.慢慢总结.

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/326756.htmlTechArticle创建数组就不再说了 array()函数 提取数组可以用list(). 测试数组元素 ,可以用is_array() 接受一个参数,传进来的变量,如果变量是数组,返回tru...
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.