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
Parameter description:
Return value description:
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);
The output result is:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 )
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().
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);
The output result is as follows:
Array ( [0] => Array ( [id] => 1 [name] => Tom [age] => 20 ) [1] => Array ( [id] => 2 [name] => Lucy [age] => 22 ) )
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);
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 ) )
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);
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:
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!