Home >Backend Development >PHP Problem >Does the php index array have key values?

Does the php index array have key values?

青灯夜游
青灯夜游Original
2022-05-30 17:58:131742browse

php index array has key values. In PHP, whether it is an index array or an associative array, each entity in the array contains two items, namely key and value; there is no difference between the two arrays in the key value, but in the key name There is a difference: the index array uses numbers as keys, while the associative array uses strings or a mixture of strings and numbers as keys.

Does the php index array have key values?

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

The php index array has key values.

PHP arrays can be divided into two types: index arrays and associative arrays.

But whether it is an index array or an associative array, 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.

Both index arrays and associative arrays have key values. There is no difference in the key values. The difference lies in the key name:

  • The index array uses numbers as the key name

  • Associative arrays use strings or a mixture of strings and numbers as keys.

Explanation:

The subscript (key name) of the index array consists of numbers, starting from 0 by default, each number Corresponding 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.

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

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$arr1=array(2,34,5,68,0,5);
var_dump($arr1);
$arr2=array(1=>"1","a"=>"red",2=>"2","b"=>"green","c"=>"blue");
var_dump($arr2);
?>

Does the php index array have key values?

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of Does the php index array have key values?. 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