PHP array_push()

王林
发布: 2024-08-29 12:45:37
原创
626 人浏览过

PHP 编程语言的 array_push() 函数实际上是一个内置函数,它有助于根据我们的要求将新元素推送到特定的数组中。我们可以根据需要将一个或多个元素推送到特定数组中,这些数组元素将插入到最后一个节/索引值位置。由于使用 array_push() 函数,特定数组的长度将根据推入特定数组的元素数量而增加/增加。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

语法和参数

PHP array_push() 的语法和参数为:

array_push($array1, $value1, $value2, $value3, …..)
登录后复制

array_push()函数的参数说明:

PHP 编程语言的 array_push() 函数内部将有多个可用参数。 array_push() 函数的参数数量基本上取决于实际推入特定数组的元素数量。具体可以将这些参数分为两类。它们是 1. $array1, 2. 值列表

  • array_push() 函数的$array1 参数: array_push() 函数的$array1 参数实际上是实际指定或操作的原始数组。它是主数组,包含之前定义的所有数组元素。
  • 值列表(多个值参数):值列表是 PHP 编程语言的 array_push() 函数的多个参数。该参数是一堆实际上用逗号分隔的元素列表,这些分隔的元素将被推入某个特定的数组中。让这些数组为 $value1、$value2、$Value3、$Value4 等等。
  • array_push() 函数的返回值: PHP 编程语言的 array_push() 函数只会通过引用的参数值添加/推送一些元素来返回修改后的数组到 array_push() 函数内部。添加的这些元素将根据我们的要求放置在一个/多个数组的最后一个索引值处。

array_push() 函数在 PHP 中如何工作?

PHP 编程语言的 array_push() 函数基本上只是将一些元素推入特定数组。 array_push() 函数还可以将多个元素推送到实际在 array_push() 函数内部指定的原始数组中。使其工作后,数组的长度将增加,并且基于推入数组的元素数量。如果数组具有键和值对,则该方法将尝试将数字键添加到推送的值中。 PHP 的 array_push() 函数仅在 PHP 4、PHP 5 和 PHP 7 版本上运行。

示例#1

这是借助原始数组参数和值列表参数说明 array_push() 函数的示例。首先在 PHP 标签


内部标签用于水平线。之后,在 array() 函数的帮助下使用一些字符串数组索引值/元素创建一个数组变量,但这里的键没有定义。然后原始数组元素将在“print_r()”函数的帮助下打印。然后创建一些值变量并在其中存储一些字符串值。这里创建了六个带有值的字符串变量。然后 array_push() 函数与原始变量和传递给它的所有六个字符串变量一起使用。这会将所有提到的元素推送到特定数组中。然后 print_r($array1) 函数将打印包含所有额外元素的数组。

代码:

<?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>";
?>
登录后复制

输出:

PHP array_push()

示例#2

这个示例与示例 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>";
?>
登录后复制

输出:

PHP array_push()

Example #3

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 array_push()的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
php
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!