PHP/Data Structure/array remove — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 07:02, 26 мая 2010

Removing One or More Arbitrary Array Elements

 
<?php
 
  $languages = array( "French", "German", "Russian", "Chinese",
                      "Hindi", "Quechua", "Spanish", "Hausa");
  printf("<pre>Original array:\n%s</pre>\n", var_export($languages, TRUE));

  $num = 2;
  
  $removed1 = array_remove($languages, 0, $num);
  $removed2 = array_remove($languages, count($languages) - $num, $num);
  printf("<p>Removed (start): %s<br />Removed (end): %s<br />Remaining: %s</p>\n",
          var_export($removed1, TRUE),
          var_export($removed2, TRUE),
          var_export($languages, TRUE));
?>



Using the array_remove() function to remove several elements from the beginning and the end of an array:

 
<?php 
$languages = array( "French", "German", "Russian", "Chinese"); 
printf("<pre>Original array:\n%s</pre>\n", var_export($languages, TRUE)); 
$num = 2; 
$removed1 = array_remove($languages, 0, $num); 
$removed2 = array_remove($languages, count($languages) - $num, $num); 
printf("<p>Removed (start): %s<br />Removed (end): %s<br /> Remaining: %s</p>\n", var_export($removed1, TRUE), var_export($removed2, TRUE), var_export($languages, TRUE)); 
?>