PHP/Data Structure/array splice

Материал из Web эксперт
Версия от 07:02, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

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);
?>