Home > Backend Development > PHP Problem > What is the unique identifier in PHP array?

What is the unique identifier in PHP array?

青灯夜游
Release: 2023-03-16 20:12:02
Original
2439 people have browsed it

The only identifier in a PHP array is the key. In PHP, each element of an array is distinguished by a special identifier called a key (also called a subscript). In a PHP array, the key can be a number or a string. An array with a pure number as the key name is an index array, and an array with a string or a mixture of strings and numbers as the key name is an associative array.

What is the unique identifier in PHP array?

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

In PHP, an array is a group of An ordinal variable where each value is called an element. Each element is distinguished by a special identifier called a key (also called a subscript).

Simply put: Each entity in the array contains two items, namely key and value; the key can be a number or a string.

What is the unique identifier in PHP array?

In a PHP array, no matter what type of key name there is, there will be a value corresponding to it, that is, a key/value pair. Depending on the data type of the array key name, We can divide PHP arrays into two types: index arrays and associative arrays

1. The one with pure numbers as the key name is the index array

The subscript of the index array ( The key name) consists of numbers, starting from 0 by default. Each 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 start from this value. incrementally.

Example 1:

<?php
header("Content-type:text/html;charset=utf-8");
$array[] = &#39;香蕉&#39;;
$array[] = &#39;苹果&#39;;
$array[] = &#39;橘子&#39;;
$array[] = &#39;榴莲&#39;;
//输出语句
var_dump($array);
?>
Copy after login

What is the unique identifier in PHP array?

Example 2:

<?php
header("Content-type:text/html;charset=utf-8");
$array= array("香蕉","苹果","梨子","橙子","橘子","榴莲");
//输出语句
var_dump($array);
?>
Copy after login

What is the unique identifier in PHP array?

2. An array in which strings or strings and numbers are mixed as key names is an associative array

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

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);  
$arr = array(&#39;Apple&#39; => &#39;苹果&#39;,&#39;Banana&#39; => &#39;香蕉&#39;,&#39;Orange&#39; => &#39;橘子&#39;,&#39;Plum&#39; => &#39;李子&#39;,&#39;Strawberry&#39; => &#39;草莓&#39;);
var_dump($arr);
?>
Copy after login

What is the unique identifier in PHP array?

The key name of an associative array can be any integer or string. If the key name is a string, add a delimiting modifier to the key name - single quote '' or double quote "". 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 the unique identifier in PHP array?. 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