Home > Backend Development > PHP Problem > Do array keys in php have to be numbers?

Do array keys in php have to be numbers?

青灯夜游
Release: 2023-03-15 21:42:01
Original
2997 people have browsed it

In PHP, array keys do not have to be numbers, they can also be strings. PHP arrays are more flexible. They not only support index arrays with integers as keys, but also support associative arrays with strings or a mixture of strings and numbers as keys. If the array key is a string, you need to add The previous delimiting modifier (single quote '' or double quote "").

Do array keys in php have to be numbers?

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

In php, the array key is not Must be a number, but can also be a string.

Array An array is an ordered set of variables in which each value is called an element. Each element is distinguished by a special identifier called a key (also called a subscript).

Each entity in the array contains two items, namely key and value. The corresponding array elements can be obtained by key value. These keys can be numeric keys or association keys. If a variable is a container that stores a single value, then an array is a container that stores multiple values.

PHP arrays are more flexible than arrays in other high-level languages. They not only support index arrays with numbers as keys, but also support associative arrays with strings or a mixture of strings and numbers as keys.

1) Index array--numeric key name

The subscript (key name) of the index array consists of numbers, starting from 0 by default, each The number corresponds to the position of an array element in the array. There is no need to specify it. 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);
Copy after login

Do array keys in php have to be numbers?

2) Associative array--String key name

The subscript of the associative array (key name ) consists of a mixture of numeric values ​​and strings. 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);
Copy after login

Do array keys in php have to be numbers?

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 Do array keys in php have to be numbers?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template