Home Backend Development PHP Problem How to put values ​​into array in PHP

How to put values ​​into array in PHP

Apr 19, 2023 am 09:21 AM

PHP is a widely used programming language. It is easy to learn and has elegant and concise code. It can easily realize various needs. In PHP, an array is a commonly used data structure that can store multiple values ​​and operate on them as needed. This article will introduce how to put values ​​into arrays in PHP.

1. Declare arrays

In PHP, there are many ways to declare arrays. The following are the three most commonly used methods:

1. Use the array() function

array() function can be used to create a new array. Here is an example to create an array with three elements:

$my_array = array("apple", "banana", "orange");
Copy after login

2. Use square brackets

You can also use square brackets to declare an array and assign values ​​to it as needed. The following example demonstrates how to create an array containing four elements:

$my_array[0] = "apple";
$my_array[1] = "banana";
$my_array[2] = "orange";
$my_array[3] = "grape";
Copy after login

3. Using a simple array variable declaration

Using a simple array variable declaration, multiple values ​​can be assigned to the array at once . The following example creates an array containing three elements:

$my_array = ["apple", "banana", "orange"];
Copy after login

2. Add values ​​to the array

After declaring the array, you need to add values ​​to the array. Values ​​can be added to an array using the following methods:

1. Using the array_push() function

The array_push() function can add one or more elements to the end of the array. The following is a simple example:

$my_array = array("apple", "banana", "orange");
array_push($my_array, "grape");
Copy after login

2. Use the subscript assignment method

You can use the subscript assignment method to add values ​​to the array. The following example demonstrates how to add a value to an array:

$my_array[4] = "grape";
$my_array[5] = "pear";
Copy after login

3. Some operations on the array

After placing the value into the array, you can perform various operations on the array as needed. The following are some common array operations in PHP:

  1. Accessing elements in an array

You can use array subscripts to access elements in an array. For example:

echo $my_array[2]; // 输出"orange"
Copy after login
  1. Get the length of the array

You can use PHP’s count() function to get the length of the array. The following is an example:

echo count($my_array); // 输出6
Copy after login
  1. Deleting elements in an array

You can use the unset() function to delete elements in an array. The following is an example:

unset($my_array[1]); // 删除数组中的"banana"元素
Copy after login

4. Loop through the array

You can use the foreach() function to loop through the array. The following is an example:

foreach ($my_array as $item) {
    echo $item."\n";
}
Copy after login

Summary

In PHP, using arrays to store multiple values ​​is very convenient and practical. This article introduces how to declare arrays, add values ​​to arrays, and some common array operations in PHP. I believe that through this article, you have learned how to use PHP to process arrays.

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1248
24