PHP/Data Structure/array unshift

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

Adding an Element to the Beginning of an Array

   <source lang="html4strict">

<?php

 $prices = array(5.95, 10.75, 11.25);
 
printf("

%s

\n", implode(", ", $prices));
 array_unshift($prices, 10.85);
printf("

%s

\n", implode(", ", $prices));
 array_unshift($prices, 3.35, 17.95);
printf("

%s

\n", implode(", ", $prices));

?>

 </source>
   
  


Adding an Element to the Beginning of an Array: int array_unshift(array $arr, mixed $val[, mixed $val2[, ...]])

   <source lang="html4strict">

<?php $prices = array(5.95, 10.75, 11.25);

printf("

%s

\n", implode(", ", $prices));

array_unshift($prices, 10.85);

printf("

%s

\n", implode(", ", $prices));

array_unshift($prices, 3.35, 17.95);

printf("

%s

\n", implode(", ", $prices));

?>

 </source>
   
  


array_unshift( ) function pushes value onto the start of the array

   <source lang="html4strict">

int array_unshift ( array &arr, mixed var [, mixed ...] ) <?

   $firstname = "Johnny";
   $names = array("Timmy", "Bobby", "Sam", "Tammy", "Joe");
   array_unshift($names, $firstname);

?>

 </source>
   
  


array_unshift() function shifts the array to the right.

   <source lang="html4strict">

//Its syntax is: int array_unshift(array array, mixed var1 [, mixed var2. . .]) //You can append one or several values simultaneously. <?

   $languages = array ("French", "Italian");
   array_unshift ($languages, "Russian", "Chinese");
   print_r($languages);

?>

 </source>