Home  >  Article  >  Backend Development  >  How to put values ​​into array in php

How to put values ​​into array in php

PHPz
PHPzOriginal
2023-04-19 09:20:33531browse

PHP language is a scripting language widely used in the field of web development. It has many built-in powerful functions and operators that can help us complete various operations easily.

In PHP, we often need to use arrays to store multiple values, and putting values ​​into an array is also a basic operation. Let's explain in detail how to put values ​​into an array in PHP.

  1. Use the array() function to create an array

PHP’s built-in array() function can be used to create a new array. When using this function, we simply list the values ​​we want to put into the array in parentheses. For example:

$my_array = array("apple", "banana", "pear");

The above code will create an array containing 3 elements, namely "apple", "banana" and "pear".

  1. Directly use square brackets [] to add elements

In PHP 5.4 and above, we can directly use square brackets [] to add elements to an array. The specific method is to put the value of the element in square brackets and use the equal sign = to assign it to the array. For example:

$my_array[] = "apple";
$my_array[] = "banana";
$my_array[] = "pear";

The above code has the same effect as the first example.

  1. Add elements using array built-in functions

There are many built-in functions in PHP that can be used to add elements to arrays, such as array_push(), array_unshift(), etc. . These functions are relatively flexible to use, and you can choose which function to use according to different needs.

The array_push() function can add one or more elements to the end of the array. When using this function, we need to put the reference of the array and the element to be added within the brackets of the function. For example:

$my_array = array("apple", "banana");
array_push($my_array, "pear", "orange");

The above code will add two elements at the end of the $my_array array, namely "pear" and "orange".

The array_unshift() function can add one or more elements at the beginning of the array, and its usage is similar to array_push(). For example:

$my_array = array("apple", "banana");
array_unshift($my_array, "pear", "orange");

The above code will add two elements at the beginning of the $my_array array, namely "pear" and "orange".

Summary:

In PHP, there are many ways to put values ​​into arrays, and we can choose which method to use based on actual needs. Among them, the most common method is to use the array() function to create an array, and to directly use square brackets [] to add elements. At the same time, you also need to master some commonly used built-in functions, such as array_push(), array_unshift(), etc., so that you can process arrays more flexibly.

The above is the detailed content of How to put values ​​into array in php. 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