Get the App
SLTechnology News&Howtos  ›  Development  › 

How to add elements to php array Array

Shulou Source: shulou.com Published: 2022-06-02 22:32:53 09月28日 Update

This article focuses on "how to add elements to the php array array". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to add elements to php array arrays.

Add method: 1, use "array_unshift ($arr, value)" statement; 2, use "array_push ($arr, value)" statement; 3, use "array_pad ($arr, array length, value)" statement; 4, use "array_splice ($arr,0,0, value)".

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Add elements to the php array array

1. Use the array_unshift () function to insert one or more new elements at the beginning of the array

Output result:

2. Use the array_push () function to insert one or more new elements at the end of the array

The output is as follows:

3. Use the array_pad () function to insert new elements into the array

The array_pad ($array,$size,$value) function inserts a key value of $value into the array $array, thus filling the array to a specified length of $size. The $size parameter can be understood as the final number of elements in the array, that is, the length of the array after the insert operation.

The output is as follows:

4. Use the array_splice () function to insert new elements into the array

The array_splice ($array,$start,$length,$value) function is a powerful function that can be used to delete array elements, replace array elements, or insert array elements (just set the parameter $length to 0).

When $length=0, the parameter $start specifies where to start the insertion (subscript), and the parameter $value specifies the insertion value (if it is multiple values, it needs to be set to an array).

The output is as follows:

At this point, I believe you have a deeper understanding of "how to add elements to the php array array". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Array element function parameter result output multiple statement length content method learning practical powerful deeper subscript location interest practicality practice Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Huawei MariaDB Shulou Technology NVidia