PHP/Data Structure/array splice — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 07:02, 26 мая 2010
Содержание
- 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
<?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("<p>Original array:</p><pre>%s</pre>\n", var_export($languages, TRUE));
$removed = array_remove($languages, 2);
printf("<p>Removed: %s<br />Remaining:</p><pre>%s</pre>\n",
var_export($removed, TRUE), var_export($languages, TRUE));
$removed = array_remove($languages, 0, 3);
printf("<p>Removed: %s<br />Remaining:</p><pre>%s</pre>\n",
var_export($removed, TRUE), var_export($languages, TRUE));
?>
array_splice
<?php
$states = array("Alabama", "Alaska", "Arizona", "Arkansas");
$subset = array_splice($states, 4);
print_r($states);
print_r($subset);
?>
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.
//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);
?>
Inserting New Values at an Arbitrary Point in an Indexed Array
<?php
function array_insert(&$array, $offset, $new)
{
array_splice($array, $offset, 0, $new);
}
$languages = array("German", "French", "Spanish");
printf("<pre>%s</pre>\n", var_export($languages, TRUE));
array_insert($languages, 1, "Russian");
printf("<pre>%s</pre>\n", var_export($languages, TRUE));
array_insert($languages, 3, array("Swedish", "Italian"));
printf("<pre>%s</pre>\n", var_export($languages, TRUE));
$languages = array("German", "French", "Spanish");
printf("<pre>%s</pre>\n", var_export($languages, TRUE));
array_insert($languages, 6, "Russian");
printf("<pre>%s</pre>\n", var_export($languages, TRUE));
$languages = array("German", "French", "Spanish");
printf("<pre>%s</pre>\n", var_export($languages, TRUE));
$languages[6] = "Russian";
printf("<pre>%s</pre>\n", var_export($languages, TRUE));
?>
Inserting New Values to an Array: array array_splice(array $original, int $offset, int $length, array $new)
<?php
function array_insert(&$array, $offset, $new) {
array_splice($array, $offset, 0, $new);
}
$languages = array("German", "French", "Spanish");
printf("<pre>%s</pre>\n", var_export($languages, TRUE));
array_insert($languages, 1, "Russian");
printf("<pre>%s</pre>\n", var_export($languages, TRUE));
array_insert($languages, 3, array("Swedish", "Italian"));
printf("<pre>%s</pre>\n", var_export($languages, TRUE));
?>
Remove all elements from positions 3 to (n 3):
<?
$pasta = array ("1", "2", "3","4","5","6","7");
$pasta = array_splice($pasta, 5, -3);
print_r( $pasta);
?>
Remove a portion of the array and replace it with something else
//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"));
?>
Remove the fifth and sixth elements from the array:
<?
$pasta = array ("1", "2", "3","4","5","6","7");
$pasta = array_splice($pasta, 5, 2);
print_r($pasta);
?>
Replace the third and fourth elements with new elements:
<?
$pasta = array ("1", "2", "3","4","5","6","7");
$pasta = array_splice($pasta, 5, 2, array("element1", "element2"));
print_r($pasta);
?>