The role of array_push in php

下次还敢
Release: 2024-04-29 09:57:16
Original
851 people have browsed it

array_push() function adds one or more elements to the end of the array and returns the new array length. The specific steps are as follows: Accept an array and the value to be added as parameters. Add the value to the end of the array. Returns the new array length, including newly added elements.

The role of array_push in php

The role of array_push()

array_push() is a function used to add one or more items to an array. function of the element. It adds the new element to the end of the array and returns the length of the new array.

Working principle

array_push() function accepts two parameters:

  • $array: To which Adding an array of elements
  • ...$values: One or more values ​​to add to the array

If multiple values ​​are provided, they Will be added to the array in the specified order.

Syntax

<code class="php">int array_push($array, ...$values)</code>
Copy after login

Return value

This function returns the length of the new array, including newly added elements.

Example

<code class="php">$arr = ['a', 'b', 'c'];

array_push($arr, 'd'); // 数组变为 ['a', 'b', 'c', 'd']

echo array_push($arr, 'e', 'f'); // 输出 6,数组变为 ['a', 'b', 'c', 'd', 'e', 'f']</code>
Copy after login

Note

    ##array_push() does not modify the passed array, but returns a new array.
  • If the provided value is not a scalar (such as an object or array), it will be converted to a string and added to the array.
  • If the array is non-associative, new elements will be added to the array using consecutive keys.
  • If the array is associative, the new element will be added to the array using its key.

The above is the detailed content of The role of array_push in php. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!