Home  >  Article  >  Backend Development  >  How to create an array in PHP

How to create an array in PHP

PHPz
PHPzOriginal
2023-04-23 10:22:46414browse

In PHP, array is a very convenient and commonly used data structure. It can store multiple values ​​and these values ​​can be easily accessed and manipulated. In this article, we will discuss how to build an array in PHP.

An array is a variable that can be used to store a series of values. These values ​​can be any type of data, including numbers, strings, booleans, etc. Creating an array is easy, just use the array() function in PHP.

A simple array example:

$fruits = array("apple", "banana", "orange", "grape");

In this example, we create an array named $fruits, which contains four different fruit names. The values ​​are placed within a pair of parentheses and separated by commas. Note that there is no space between the parentheses and the comma.

We can also use subscripts to access values ​​in the array. An array subscript is an integer that uniquely identifies each element in the array. The subscripts start from 0 and increase sequentially. For example:

$fruits = array("apple", "banana", "orange", "grape");
echo $fruits[0]; // 输出 "apple"
echo $fruits[1]; // 输出 "banana"
echo $fruits[2]; // 输出 "orange"
echo $fruits[3]; // 输出 "grape"

In this example, we use array subscripts to output each element in the array. $fruits[0] represents the first element in the array, $fruits[1] represents the second element in the array, and so on.

We can also use associative arrays to create an array. An associative array is an array type in which each element has a name, called a "key". Unlike ordinary arrays, subscripts in associative arrays can be strings. For example:

$person = array(
     "name" => "John Smith",
     "age" => 30,
     "gender" => "Male"
);

In this example, we create an associative array named $person. Among them, each element consists of a key and a value. The key is a string and the value can be any type of data. We can use code similar to the following to access associative array elements:

echo $person["name"]; // 输出 "John Smith"
echo $person["age"]; // 输出 30
echo $person["gender"]; // 输出 "Male"

In addition to using the array() function, we can also use square bracket syntax to create arrays. For example, we can use the following code to create an array named $colors:

$colors = ["red", "green", "blue", "yellow"];

This method has the same effect as using the array() function to create an array, but the syntax is slightly different.

Summary

In PHP, there are many ways to create arrays. We can use array() function, square bracket syntax or associative array. Using arrays, we can easily store and manipulate a range of values. The above is the basic knowledge of establishing PHP arrays. I hope it will inspire readers.

The above is the detailed content of How to create an 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