PHP/Data Structure/array splice
Содержание
- 1 array remove
- 2 array_splice
- 3 array_splice() function replaces the designated elements specified by the offset and the optional length input parameters with the elements in the optional array replacement_array.
- 4 Inserting New Values at an Arbitrary Point in an Indexed Array
- 5 Inserting New Values to an Array: array array_splice(array $original, int $offset, int $length, array $new)
- 6 Remove all elements from positions 3 to (n 3):
- 7 Remove a portion of the array and replace it with something else
- 8 Remove the fifth and sixth elements from the array:
- 9 Replace the third and fourth elements with new elements:
array remove
<source lang="html4strict">
<?php
function array_remove(&$array, $offset, $length=1) { return array_splice($array, $offset, $length); } $languages = array( "French", "German", "Russian", "Chinese", "Hindi", "Quechua", "Spanish", "Hausa");printf("
Original array:
%s\n", var_export($languages, TRUE));
$removed = array_remove($languages, 2);printf("
Removed: %s
Remaining:
%s\n",
var_export($removed, TRUE), var_export($languages, TRUE)); $removed = array_remove($languages, 0, 3);printf("
Removed: %s
Remaining:
%s\n",
var_export($removed, TRUE), var_export($languages, TRUE));
?>
</source>
array_splice
<source lang="html4strict">
<?php $states = array("Alabama", "Alaska", "Arizona", "Arkansas"); $subset = array_splice($states, 4); print_r($states); print_r($subset); ?>
</source>
array_splice() function replaces the designated elements specified by the offset and the optional length input parameters with the elements in the optional array replacement_array.
<source lang="html4strict">
//Its syntax is: array_splice(array input_array, int offset, int [length], array[replacement_array]); //Remove all elements from the fifth element to the end of the array: <? $pasta = array ("1", "2", "3","4","5","6","7"); $pasta = array_splice($pasta, 5); print_r($pasta); ?>
</source>
Inserting New Values at an Arbitrary Point in an Indexed Array
<source lang="html4strict">
<?php
function array_insert(&$array, $offset, $new) { array_splice($array, $offset, 0, $new); } $languages = array("German", "French", "Spanish");printf("
%s\n", var_export($languages, TRUE));
array_insert($languages, 1, "Russian");printf("
%s\n", var_export($languages, TRUE));
array_insert($languages, 3, array("Swedish", "Italian"));printf("
%s\n", var_export($languages, TRUE));
$languages = array("German", "French", "Spanish");printf("
%s\n", var_export($languages, TRUE));
array_insert($languages, 6, "Russian");printf("
%s\n", var_export($languages, TRUE));
$languages = array("German", "French", "Spanish");printf("
%s\n", var_export($languages, TRUE));
$languages[6] = "Russian";printf("
%s\n", var_export($languages, TRUE));
?>
</source>
Inserting New Values to an Array: array array_splice(array $original, int $offset, int $length, array $new)
<source lang="html4strict">
<?php function array_insert(&$array, $offset, $new) {
array_splice($array, $offset, 0, $new);
} $languages = array("German", "French", "Spanish");
printf("%s\n", var_export($languages, TRUE));
array_insert($languages, 1, "Russian");
printf("%s\n", var_export($languages, TRUE));
array_insert($languages, 3, array("Swedish", "Italian"));
printf("%s\n", var_export($languages, TRUE));
?>
</source>
Remove all elements from positions 3 to (n 3):
<source lang="html4strict">
<? $pasta = array ("1", "2", "3","4","5","6","7"); $pasta = array_splice($pasta, 5, -3); print_r( $pasta); ?>
</source>
Remove a portion of the array and replace it with something else
<source lang="html4strict">
//array_splice() examples <? $input = array("red", "green", "blue", "yellow"); array_splice($input, 2); array_splice($input, 1, -1); array_splice($input, 1, count($input), "orange"); array_splice($input, -1, 1, array("black", "maroon")); ?>
</source>
Remove the fifth and sixth elements from the array:
<source lang="html4strict">
<? $pasta = array ("1", "2", "3","4","5","6","7"); $pasta = array_splice($pasta, 5, 2); print_r($pasta); ?>
</source>
Replace the third and fourth elements with new elements:
<source lang="html4strict">
<? $pasta = array ("1", "2", "3","4","5","6","7"); $pasta = array_splice($pasta, 5, 2, array("element1", "element2")); print_r($pasta); ?>
</source>