Difference between array_slice and array_splice in php
Posted by Raj
Difference between array_slice() and array_splice() in php
array_splice() is used to remove some elements from an array and replace with specified elements.
array_slice() is used to extract a selected parts of an array .
array_slice() Example:
<?php
$numbers = array("1", "2", "3", "4", "5");
$new_numbers = array_slice($numbers, 2);
$new_numbers = array_slice($numbers, -3, 1);
?>
Output:
Array
(
[0] => 3
[1] => 4
[2] => 5
)
Array
(
[0] => 3
)
array_splice() Example:
<?php
$numbers = array("1", "2", "3", "4", "5");
$new_numbers = array_splice($numbers, 2);
$new_numbers = array_splice($numbers, -3, 1,"8");
?>
Output:
Array
(
[0] => 1
[1] => 2
)
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 8
)
array_splice() is used to remove some elements from an array and replace with specified elements.
array_slice() is used to extract a selected parts of an array .
array_slice() Function in PHP:
array_slice() is used to extract a selected parts of an array .array_slice() Example:
<?php
$numbers = array("1", "2", "3", "4", "5");
$new_numbers = array_slice($numbers, 2);
$new_numbers = array_slice($numbers, -3, 1);
?>
Output:
Array
(
[0] => 3
[1] => 4
[2] => 5
)
Array
(
[0] => 3
)
array_splice() Function in PHP:
array_splice() is used to remove some elements from an array and replace with specified elements.array_splice() Example:
<?php
$numbers = array("1", "2", "3", "4", "5");
$new_numbers = array_splice($numbers, 2);
$new_numbers = array_splice($numbers, -3, 1,"8");
?>
Output:
Array
(
[0] => 1
[1] => 2
)
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 8
)
This entry was posted on October 4, 2009 at 12:14 pm, and is filed under
difference between array_slice() and array_splice() in php,
PHP,
php array_slice,
php array_splice,
PHP interview questions and answers
.You can leave a response, or trackback from your own site.