PHP 编程语言的 array_push() 函数实际上是一个内置函数,它有助于根据我们的要求将新元素推送到特定的数组中。我们可以根据需要将一个或多个元素推送到特定数组中,这些数组元素将插入到最后一个节/索引值位置。由于使用 array_push() 函数,特定数组的长度将根据推入特定数组的元素数量而增加/增加。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
PHP array_push() 的语法和参数为:
array_push($array1, $value1, $value2, $value3, …..)
array_push()函数的参数说明:
PHP 编程语言的 array_push() 函数内部将有多个可用参数。 array_push() 函数的参数数量基本上取决于实际推入特定数组的元素数量。具体可以将这些参数分为两类。它们是 1. $array1, 2. 值列表
PHP 编程语言的 array_push() 函数基本上只是将一些元素推入特定数组。 array_push() 函数还可以将多个元素推送到实际在 array_push() 函数内部指定的原始数组中。使其工作后,数组的长度将增加,并且基于推入数组的元素数量。如果数组具有键和值对,则该方法将尝试将数字键添加到推送的值中。 PHP 的 array_push() 函数仅在 PHP 4、PHP 5 和 PHP 7 版本上运行。
这是借助原始数组参数和值列表参数说明 array_push() 函数的示例。首先在 PHP 标签
代码:
<?php // PHP code which helps in illustrating the usage of array_push() function of PHP // The Input array echo "<hr>"; $array1 = array("ram", "krishna", "aakash"); echo "The array values which are present before pushing elements :: "; echo "<br>"; print_r($array1); echo "<hr>"; // elements to push $value1 = "pavan"; $value2 = "kumar"; $value3 = "sake"; $value4 = "anil"; $value5 = "maruthi"; $value6 = "raj"; echo "The array values which are present after using the pushing function :: "; echo "<br>"; // This is the array which is after the pushing of some new elements array_push($array1, $value1, $value2, $value3, $value4, $value5, $value6); print_r($array1); echo "<hr>"; ?>
输出:
这个示例与示例 1 类似,但不同之处在于,在 array() 函数内部,声明/提到了 Key 和 value 参数(提到了 Key_value 对)。除此之外,一切都与示例 1 非常相似。您可以检查下面输出部分中提到的程序的输出,以便更好、更轻松地理解 array_push() 函数。
代码:
<?php // PHP code which helps in illustrating the usage of array_push() function of PHP // The Input array echo "<hr>"; $array2 = array(1=>"rahim", 2=>"krishnaveni", 3=>"lion"); echo "The array values which are present before pushing elements :: "; echo "<br>"; print_r($array2); echo "<hr>"; // elements to push $valuea1 = "pavan"; $valuea2 = "sake"; $valuea3 = "kumar"; $valuea4 = "king"; $valuea5 = "queen"; $valuea6 = "birbal"; echo "The array values which are present after using the pushing function :: "; echo "<br>"; // This is the array which is after the pushing of some new elements array_push($array2, $valuea1, $valuea2, $valuea3, $valuea4, $valuea5, $valuea6); print_r($array2); echo "<hr>"; ?>
输出:
This example is a simple illustration of the array_push() function but here only some integer values are used as the array elements. Then four variables are created with some integer values to it. Then all those four variable values are pushed into the original array with the help of array_push() function. Other than this everything is similar to example 1 and 2. You can check the output below to understand the concept of array_push() better and so easily.
Code:
<?php // PHP code which helps in illustrating the usage of array_push() function of PHP // The Input array echo "<hr>"; $array2 = array(2, 42, 8); echo "The array values which are present before pushing elements :: "; echo "<br>"; print_r($array2); echo "<hr>"; // elements to push $valuea1 = 12; $valuea2 = 13; $valuea3 = 14; $valuea4 = 15; echo "The array values which are present after using the pushing function :: "; echo "<br>"; // This is the array which is after the pushing of some new elements array_push($array2, $valuea1, $valuea2, $valuea3, $valuea4); print_r($array2); echo "<hr>"; ?>
Output:
以上是PHP array_push()的详细内容。更多信息请关注PHP中文网其他相关文章!