Home  >  Article  >  Backend Development  >  What is an array in php?

What is an array in php?

青灯夜游
青灯夜游Original
2022-05-31 20:25:212112browse

An array in php is a collection of ordered data. Each member of the array is called an element. Each element is distinguished by a special identifier, which is called a key name (or subscript). PHP arrays can be divided into two types according to different key names: 1. Index arrays with numbers as key names; 2. Associative arrays with strings or a mixture of strings and numbers as key names.

What is an array in php?

The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer

The array in php is a group of A collection of sequential data organizes a series of data to form an operable whole.

Each member in the array is called an element, and each element is distinguished by a special identifier, which is called a key (also called a subscript).

What is an array in php?

  • The length of the array is flexible and readable and writable.

  • Can store any number of data of any type

PHP array type

In In a PHP array, no matter what type of key name there will be a value corresponding to it, that is, a key/value pair. According to the different data types of the array key names, we can divide PHP arrays into two types:

  • An array with numbers as keys is called an Indexed Array;

  • An array with strings or a mixture of strings and numbers as keys is called an association Array (Associative Array).

1) Index array--numeric key name

The subscript (key name) of the index array consists of numbers, starting from 0 by default, and each number corresponds to one The position of the array element in the array does not need to be specified. PHP will automatically assign an integer value to the key name of the index array, and then automatically increase from this value. As shown below:

$arr=array(1,2,3,4,5,6,7,8,9,10);

What is an array in php?

2) Associative array--string key name

The subscript (key name) of the associative array consists of a numerical value and a string It is composed of mixed forms. If a key name in an array is not a number, then the array is an associative array. As shown below:

$arr=array("id"=>1,"name"=>"李华","age"=>23,"1"=>1,"id2"=>52);

What is an array in php?

Note: The key name of the associative array can be any integer or string. If the key name is a string, add a delimiting modifier to the key name - single quotes ' ' or double quotes " ". For indexed arrays, in order to avoid confusion, it is best to add delimiters.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What is 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