PHP: array_splice

by Prabakaran 2012-05-24 10:45:40


Php: array_splice


Description

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




Exapmle
<?php

/************** PHP Array Splice Example ******************/

$input = array("red", "green", "blue", "yellow");

echo "<pre>Before Splice Input Array<br>";print_r($input);echo "</pre>";

array_splice($input, 2);

// $input is now array("red", "green")

echo "<pre>After Splice Input Array<br>";print_r($input);echo "</pre>";

echo "-----------------------------------------<br>";

$input1 = array("A", "B", "C", "D");

echo "<pre>Before Splice Input1 Array<br>";print_r($input1);echo "</pre>";

array_splice($input1, 1, -1);

// $input is now array("red", "yellow")

echo "<pre>After Splice Input1 Array<br>";print_r($input1);echo "</pre>";

echo "-----------------------------------------<br>";

$input2 = array("H", "I", "O", "X");

echo "<pre>Before Splice Input2 Array<br>";print_r($input2);echo "</pre>";

array_splice($input2, 1, count($input2), "INDIA");

// $input is now array("red", "orange")

echo "<pre>After Splice Input2 Array<br>";print_r($input2);echo "</pre>";

echo "-----------------------------------------<br>";

$input3 = array("Samsung", "Sony", "Nokia", "LG");

echo "<pre>Before Splice Input3 Array<br>";print_r($input3);echo "</pre>";

array_splice($input3, -1, 1, array("Apple", "Motorola"));

// $input is now array("red", "green",

// "blue", "black", "maroon")

echo "<pre>After Splice Input3 Array<br>";print_r($input3);echo "</pre>";

echo "-----------------------------------------<br>";

$input4 = array("Alto", "Honda City", "volkswagen", "BMW");

echo "<pre>Before Splice Input4 Array<br>";print_r($input4);echo "</pre>";

array_splice($input4, 3, 0, "Ferrari");

// $input is now array("red", "green",

// "blue", "purple", "yellow");

echo "<pre>After Splice Input4 Array<br>";print_r($input4);echo "</pre>";

echo "-----------------------------------------<br>";

?>

1057
like
0
dislike
0
mail
flag

You must LOGIN to add comments