当前位置:  首页>> 技术小册>> PHP合辑3-数组函数

PHP的这个内置函数用于删除或弹出并返回作为参数传递给它的数组的最后一个元素。由于从数组中删除了最后一个元素,因此数组的大小减少了一个。

语法:

array_pop($array)

参数:该函数只有一个参数$array,即输入数组,并从中弹出最后一个元素,使数组的大小减少一个。

返回值:该函数返回数组的最后一个元素。如果数组为空或输入参数不是数组,则返回NULL。

示例:

  1. Input : $array = (1=>"ram", 2=>"krishna", 3=>"aakash");
  2. Output : aakash
  3. Input : $array = (24, 48, 95, 100, 120);
  4. Output : 120

下面的程序说明了PHP中的array_pop()函数:

  1. <?php
  2. // PHP code to illustrate the use of array_pop()
  3. $array = array(1=>"ram", 2=>"krishna", 3=>"aakash");
  4. print_r("Popped element is ");
  5. echo array_pop($array);
  6. print_r("\nAfter popping the last element, ".
  7. "the array reduces to: \n");
  8. print_r($array);
  9. ?>

output:

  1. Popped element is aakash
  2. After popping the last element, the array reduces to:
  3. Array
  4. (
  5. [1] => ram
  6. [2] => krishna
  7. )

示例2:

  1. <?php
  2. $arr = array(24, 48, 95, 100, 120);
  3. print_r("Popped element is ");
  4. echo array_pop($arr);
  5. print_r("\nAfter popping the last element, ".
  6. "the array reduces to: \n");
  7. print_r($arr);
  8. ?>

output:

  1. Popped element is 120
  2. After popping the last element, the array reduces to:
  3. Array
  4. (
  5. [0] => 24
  6. [1] => 48
  7. [2] => 95
  8. [3] => 10
  9. )

该分类下的相关小册推荐: