PHP/Data Structure/array push
Содержание
- 1 Adding an Element to the End of an Array
- 2 Adding array elements
- 3 array_push() function appends one or more values onto the end of the array.
- 4 array_push( ) pushes value onto the end of the array
- 5 Push one or more elements onto the beginning of array
- 6 Push one or more elements onto the end of array
Adding an Element to the End of an Array
<source lang="html4strict">
<?php
$languages = array(); $languages[] = "German"; $languages[] = "French"; $languages[] = "Spanish";printf("
Languages: %s.
\n", implode(", ", $languages));?> <?php
$languages = array(); array_push($languages, "German", "French", "Spanish");printf("
Languages: %s.
\n", implode(", ", $languages));?>
</source>
Adding array elements
<source lang="html4strict">
<html>
<body>
-
<?php
$arr = array( "Red ","Green ","Blue" );
array_unshift( $arr, "Cyan", "Magenta" );
array_push( $arr, "Yellow", "Black" );
foreach( $arr as $value)
{
echo( "
- Do you like $value ? ");
} ?>
</body>
</html>
</source>
array_push() function appends one or more values onto the end of the array.
<source lang="html4strict">
//Its syntax is: int array_push(array array, mixed var, [. . . ]) <?
$languages = array("A", "B", "C"); array_push($languages, "Russian", "German", "Gaelic"); print_r($languages);
?> == array_pop() function removes a value from the end of the array. This value is then returned. //Its syntax is: mixed array_pop(array array) //Each iteration of array_pop() will shorten the length of the array by 1. <?
$languages = array ("Spanish", "English", "French", "Gaelic"); $a_language = array_pop ($languages); print $a_language; $a_language = array_pop ($languages); print_r( $a_language);
?>
</source>
array_push( ) pushes value onto the end of the array
<source lang="html4strict">
//int array_push ( array &arr, mixed var [, mixed ...] ) <?
$firstname = "Johnny"; $names = array("Timmy", "Bobby", "Sam", "Tammy", "Joe"); array_push($names, $firstname);
?>
</source>
Push one or more elements onto the beginning of array
<source lang="html4strict">
<? $queue = array("p1", "p3"); array_unshift($queue, "p4", "p5", "p6"); ?>
</source>
Push one or more elements onto the end of array
<source lang="html4strict">
<? $stack = array (1, 2); array_push($stack, "+", 3); ?>
</source>