PHP array function application array_push()

WBOY
Release: 2023-06-20 06:22:01
Original
7122 people have browsed it

In PHP programming, arrays are a very important data type, and PHP also provides a wealth of array functions for developers to use. One of the commonly used array functions is array_push(). This article will introduce the usage and application scenarios of this function.

1. Definition of array_push()

The array_push() function is used to add one or more elements to the end of the array and return the length of the array after adding the elements. The definition of this function is as follows:

array_push ( array &$array , mixed $value1 [, mixed $... ] ) : int
Copy after login

Parameter description:

  • array &$array: required parameter, the target array of elements to be added.
  • mixed $value 1: Required parameter, the first element to be added.
  • mixed $…: Optional parameter, the second and subsequent elements to be added.

Return value description:

  • int: The length of the array after adding elements.

2. Usage of array_push()

The usage of array_push() function is very simple. You only need to pass in the array of elements to be added and the element value. The following is a simple example:

$arr = array(1, 2, 3);
array_push($arr, 4, 5, 6);
print_r($arr);
Copy after login

The output result is:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
)
Copy after login

3. Application scenarios of array_push()

array_push() function is often used to add elements at the end of an array . Here are some application scenarios of array_push().

  1. Add new elements to the array

When we need to add new elements to an array, we can use the array_push() function. For example, we have an array used to store user information, and we need to add new user information to the array:

$user_info = array();
$user1 = array('id'=>1, 'name'=>'Tom', 'age'=>20);
$user2 = array('id'=>2, 'name'=>'Lucy', 'age'=>22);
array_push($user_info, $user1, $user2);
print_r($user_info);
Copy after login

The output result is as follows:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Tom
            [age] => 20
        )

    [1] => Array
        (
            [id] => 2
            [name] => Lucy
            [age] => 22
        )

)
Copy after login
  1. Add to the two-dimensional array New Row

Sometimes, we need to add a new row to a two-dimensional array. For example, we have a two-dimensional array used to store student performance information, and we need to add new rows to the array:

$score_info = array();
$score1 = array('id'=>1, 'name'=>'Tom', 'math'=>90, 'english'=>80);
$score2 = array('id'=>2, 'name'=>'Lucy', 'math'=>85, 'english'=>90);
array_push($score_info, $score1, $score2);
$score3 = array('id'=>3, 'name'=>'Jack', 'math'=>95, 'english'=>95);
array_push($score_info, $score3);
print_r($score_info);
Copy after login

The output result is as follows:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Tom
            [math] => 90
            [english] => 80
        )

    [1] => Array
        (
            [id] => 2
            [name] => Lucy
            [math] => 85
            [english] => 90
        )

    [2] => Array
        (
            [id] => 3
            [name] => Jack
            [math] => 95
            [english] => 95
        )

)
Copy after login
  1. In the loop, add Adding elements to an array

Sometimes, we need to add elements to an array in a loop. For example, we need to query user information from the database and store the results in an array:

$user_info = array();
$result = mysql_query("SELECT * FROM user");
while ($row = mysql_fetch_assoc($result)) {
    array_push($user_info, $row);
}
print_r($user_info);
Copy after login

The output results are in the form of an array of query results.

4. Notes

When using the array_push() function, you need to pay attention to the following:

  1. The array_push() function can only add elements to the end.
  2. You can add any type of data to the array, including numbers, strings, arrays, objects, etc.
  3. When you need to add a single element to an array, you can use array[] = $value, which is more efficient than the array_push() function.

In short, the array_push() function is a very practical array function and is widely used in PHP development. When using this function, you need to pay attention to its usage and precautions to avoid errors.

The above is the detailed content of PHP array function application array_push(). For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template