Ds \ Vector::unshift()函數是PHP中的內置函數,用於將元素添加到向量的前麵。此函數將向量中的所有元素都向前移動,並將新元素添加到前麵。
用法:
void public Ds\Vector::unshift( $values )
參數:該函數接受單個參數$values,該參數保存要添加到向量中的值。
返回值:該函數不返回任何值。
以下示例程序旨在說明PHP中的Ds \ Vector::unshift()函數:
程序1:
<?php
// Create new Vector
$vect = new \Ds\Vector([3, 6, 1, 2, 9, 7]);
echo("Original vector:\n");
// Display the vector elements
print_r($vect);
echo("\nArray elements after inserting new element\n");
// Use unshift() function to aff elements
$vect->unshift(10, 20, 30);
// Display updated vector elements
print_r($vect);
?>輸出:
Original vector:
Ds\Vector Object
(
[0] => 3
[1] => 6
[2] => 1
[3] => 2
[4] => 9
[5] => 7
)
Array elements after inserting a new element
Ds\Vector Object
(
[0] => 10
[1] => 20
[2] => 30
[3] => 3
[4] => 6
[5] => 1
[6] => 2
[7] => 9
[8] => 7
)
程序2:
<?php
// Create new Vector
$vect = new \Ds\Vector(["geeks", "for", "geeks"]);
echo("Original vector:\n");
// Display the vector elements
print_r($vect);
echo("\nArray elements after inserting new element\n");
// Use unshift() function to aff elements
$vect->unshift("PHP articles");
// Display updated vector elements
print_r($vect);
?>輸出:
Original vector:
Ds\Vector Object
(
[0] => geeks
[1] => for
[2] => geeks
)
Array elements after inserting a new element
Ds\Vector Object
(
[0] => PHP articles
[1] => geeks
[2] => for
[3] => geeks
)
參考: http://php.net/manual/en/ds-vector.unshift.php
相關用法
- Node.js unshift()用法及代碼示例
- PHP Ds\Sequence unshift()用法及代碼示例
- PHP Ds\Deque unshift()用法及代碼示例
- PHP SplDoublyLinkedList unshift()用法及代碼示例
- PHP end()用法及代碼示例
- p5.js hex()用法及代碼示例
- CSS var()用法及代碼示例
- p5.js str()用法及代碼示例
- PHP tan( )用法及代碼示例
- p5.js arc()用法及代碼示例
- p5.js red()用法及代碼示例
- p5.js log()用法及代碼示例
注:本文由純淨天空篩選整理自barykrg大神的英文原創作品 PHP | Ds\Vector unshift() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
