Home  >  Article  >  Backend Development  >  What do array elements in PHP consist of?

What do array elements in PHP consist of?

青灯夜游
青灯夜游Original
2022-06-29 16:37:413863browse

Array elements in PHP are composed of "key name" and "key value". Each array element in the array contains two items, namely key and value. The key name is also called a subscript, which can be a numeric type or a string type; according to the different data types of the array key names, the array can be divided into two types: index arrays with numbers as key names, string or An array in which strings and numbers are mixed as keys is called an associative array.

What do array elements in PHP consist of?

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

Each entity (element) in the array is Contains two items, namely key and value.

What do array elements in PHP consist of?

The key name is also called a subscript, which can be a numeric type or a string type.

In a PHP array, no matter what type of key name there is a value corresponding to it, that is, a key/value pair.

According to the different data types of array key names, we can divide PHP arrays into two types:

  • Using numbers as key names is an Indexed Array;

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

1) Index array

The subscript (key name) of the index array consists of numbers, starting from 0 by default. Each number corresponds to an array element in the array There is no need to specify the position. PHP will automatically assign an integer value to the key name of the index array, and then automatically increase from this value.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$array=array(1,2,3,4,5,6,7,8,9,10);
var_dump($array);//打印数组
?>

What do array elements in PHP consist of?

2) Associative array

The subscript (key name) of the associative array is composed of a mixture of numerical values ​​and strings. If there are If a key name is not a number, then the array is an associative array.

If the key name is a string, the key name must be wrapped with a delimiting modifier - single quote '' or double quote "".

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$array=array("id"=>1,"name"=>"李华","age"=>23,"1"=>1,"id2"=>52);
var_dump($array);//打印数组
?>

What do array elements in PHP consist of?

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What do array elements in PHP consist of?. 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