array_splice

(PHP 4, PHP 5, PHP 7)

array_splice — ȥ�������е�ijһ���ֲ�������ֵȡ��

˵��

array_splice ( array &$input , int $offset [, int $length = count($input) [, mixed $replacement = array() ]] ) : array

�� input �������� offset �� length ָ���ĵ�Ԫȥ��������ṩ�� replacement �������������еĵ�Ԫȡ����

ע�� input �е����ּ�������������

Note: ��� replacement �������飬�ᱻ ����ת�� ������ (���磺 (array) $replacement)�� ������� replacement �Ǹ�������� NULL���ᵼ��δ֪����Ϊ���֡�

����

input

��������顣

offset

��� offset Ϊ������� input �����и�ֵָ����ƫ������ʼ�Ƴ������ offset Ϊ������� input ĩβ������ֵָ����ƫ������ʼ�Ƴ���

length

���ʡ�� length�����Ƴ������д� offset ����β�����в��֡����ָ���� length ����Ϊ��ֵ�����Ƴ���ô�൥Ԫ�����ָ���� length ����Ϊ��ֵ�����Ƴ��� offset ������ĩβ���� length Ϊֹ�м����еĵ�Ԫ�� ��������� length Ϊ�㣬�����Ƴ���Ԫ�� С���ţ��������� replacement ʱҪ�Ƴ��� offset ������ĩβ���е�Ԫʱ���� count($input) ��Ϊ length��

replacement

��������� replacement ���飬���Ƴ��ĵ�Ԫ���������еĵ�Ԫ�����

��� offset �� length ����Ͻ���Dz����Ƴ��κ�ֵ���� replacement �����еĵ�Ԫ�������뵽 offset ָ����λ�á� ע���滻�����еļ�����������

��������滻 replacement ֻ��һ����Ԫ����ô����Ҫ�������� array()�����Ǹõ�Ԫ�������һ�����顢һ��������� NULL��

����ֵ

����һ�������б��Ƴ���Ԫ�����顣

����

Example #1 array_splice() ����

<?php
$input 
= array("red", "green", "blue", "yellow");
array_splice($input, 2);
// $input is now array("red", "green")

$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, -1);
// $input is now array("red", "yellow")

$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, count($input), "orange");
// $input is now array("red", "orange")

$input = array("red", "green", "blue", "yellow");
array_splice($input, -1, 1, array("black", "maroon"));
// $input is now array("red", "green",
//          "blue", "black", "maroon")

$input = array("red", "green", "blue", "yellow");
array_splice($input, 3, 0, "purple");
// $input is now array("red", "green",
//          "blue", "purple", "yellow");
?>

Example #2 array_splice() ����

���±��ʽ��ͬ����ʽ�޸��� $input��

<?php

// ���������Ԫ�ص� $input
array_push($input, $x, $y);
array_splice($input, count($input), 0, array($x, $y));

// �Ƴ� $input �е����һ��Ԫ��
array_pop($input);
array_splice($input, -1);

// �Ƴ�  $input �е�һ��Ԫ��
array_shift($input);
array_splice($input, 0, 1);

// �� $input �Ŀ�ͷ����һ��Ԫ��
array_unshift($input, $x, $y);
array_splice($input, 0, 0, array($x, $y));

// �� $input  ������  $x ���滻ֵ
$input[$x] = $y; // ���ڼ�����ƫ������ֵ������
array_splice($input, $x, 1, $y);
?>

�μ�