Home >Backend Development >PHP Problem >How to use php array_push() function? (Usage introduction)

How to use php array_push() function? (Usage introduction)

青灯夜游
青灯夜游Original
2020-09-28 15:04:423457browse

php The array_push() function is used to insert one or more elements to the end of the array and return the number of elements of the new array. The syntax is "array_push(array,value1,value2...)"; even if the array has String key name, the elements added by this function will also be numeric key names.

How to use php array_push() function? (Usage introduction)

Recommended: "PHP Video Tutorial"

array_push() function inserts one or more elements to the end of the array .

Note: Even if the array has a string key, the added element will be a numeric key

Syntax

array_push(array,value1,value2...)

Parameters:

  • array Required. Specifies an array.

  • value1 Required. Specifies the value to add.

  • value2 Optional. Specifies the value to add.

Description:

array_push() treats array as a stack and pushes the passed variables into the end of array. The length of array will increase according to the number of variables pushed onto the stack. The effect is the same as the following:

<?php
 $array[] = $var;
?>

Repeat the above action for each var, which is equivalent to performing multiple assignment operations on $array[].

Return value: Returns the number of elements of the new array.

Note:

(1) If you use array_push() to add a unit to the array, it is better to use $array[] = (directly assign a value to the array), because there is no function call. additional burden.

(2) If the first parameter is not an array, array_push() will issue a warning. This is different from the behavior of $var[], which creates a new array.

Let’s take a small example to talk about the use of array_push:

$user = array(
  0 => array(
  &#39;id&#39; => 1,
  &#39;name&#39; => &#39;张三&#39;,
  &#39;email&#39; => &#39;zhangsan@sina.com&#39;,
  ),
  1 => array(
  &#39;id&#39; => 2,
  &#39;name&#39; => &#39;李四&#39;,
  &#39;email&#39; => &#39;lisi@163.com&#39;,
  ),
  9 => array(
  &#39;id&#39; => 5,
  &#39;name&#39; => &#39;王五&#39;,
  &#39;email&#39; => &#39;10000@qq.com&#39;,
  )
 );

Assume that the two-dimensional array above is the data taken out from the database. At this time, I want to get the collection of the name column, like the following In this way:

How to use php array_push() function? (Usage introduction)

, we can use foreach combined with array_push to obtain it.

$ids = array(); 
foreach ($user as $key => $value) {
  array_push($ids,$value[&#39;name&#39;]);
}

Execute the printing function print_t(), the result is as above! Of course you can also get an array collection of any column (id, email, etc.).

Related recommendations: php training

The above is the detailed content of How to use php array_push() function? (Usage introduction). For more information, please follow other related articles on the PHP Chinese website!

Statement:
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