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:21:51912browse

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");

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

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"];

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");

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

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"
  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
  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"元素

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";
}

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