array_push_PHP array_push()函数与示例

Php (2) 2024-05-18 17:12

Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说array_push_PHP array_push()函数与示例,希望能够帮助你!!!。

array_push

PHP array_push()函数 (PHP array_push() function)

array_push() function is used to insert/push one or more than one element to the array.

array_push()函数用于将一个或多个元素插入/推入数组。

Syntax:

句法:

    array_push(array, elemement1, [element2],..);

Here,

这里,

  • array is the source array in which we have to push the elements.

    array是我们必须在其中推送元素的源数组。

  • element is the value to be added, we can add more than one elements too.

    element是要添加的值,我们也可以添加多个元素。

Examples:

例子:

    Input:
    //initial array is
    $arr = array(100, 200, 300);

    //pushing elements
    array_push($arr, 10);
    array_push($arr, 400, 500);
   
    Output:
    Array
    (    
        [0] => 100
        [1] => 200
        [2] => 300
        [3] => 10 
        [4] => 400
        [5] => 500
    )

PHP code 1: Inserting elements in an indexed array

PHP代码1:在索引数组中插入元素

<?php
    $arr = array(100, 200, 300);
    
    array_push($arr, 10);
    array_push($arr, 400, 500);
    
    print("array after inserting elements...\n");
    print_r($arr);
?>

Output

输出量

random key: age
array of random keys...
Array          
(              
    [0] => age 
    [1] => city
)

PHP code 2: Inserting elements in an associative array

PHP代码2:在关联数组中插入元素

<?php
    $arr = array("name" => "Amit", "age" => 21);
    
    array_push($arr, "Gwalior");
    array_push($arr, "Male", "RGTU University");
    
    print("array after inserting elements...\n");
    print_r($arr);
?>

Output

输出量

array after inserting elements...
Array             
(  
    [name] => Amit
    [age] => 21
    [0] => Gwalior
    [1] => Male
    [2] => RGTU University
) 

See the output – There are two keys name and age and we added three more elements "Gwalior", "Male" and "RGTU University", these three elements added with the keys 0, 1 and 2.

看到输出-有两个关键的姓名和年龄 ,我们增加了三个要素“瓜廖尔”,“ 男性”“RGTU大学”,与键0,1和2添加了这三个要素。

翻译自: https://www.includehelp.com/php/array_push-function-with-example.aspx

array_push

今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

发表回复