Home  >  Article  >  Backend Development  >  What key names can be used in php arrays

What key names can be used in php arrays

青灯夜游
青灯夜游Original
2023-01-07 18:17:404074browse

PHP arrays can use numeric key names or string key names. In PHP, it supports index arrays with numbers as keys, and associative arrays with strings or a mixture of strings and numbers as keys. The subscript (key name) of the index array 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. It then automatically increments from this value.

What key names can be used in php arrays

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

In PHP, an array is an ordered set of Variables where 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 numerical keys or associated 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.

Note: Arrays in PHP actually do not distinguish between indexes and associative arrays. The corresponding key value is found based on the key name.

1. Index array

The index array stores a series of organized single or multiple values, each of which can be Use an unsigned integer value for access.

The subscript (key name) of the index array 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 set the key of the index array. Assign an integer value to the name, and then automatically increment from this value.

Index array initialization example:

<?php  
header("Content-type:text/html;charset=utf-8");
//创建一个索引数组,索引数组的键是“0”,值是“苹果”  
$fruit=array("苹果","香蕉");  
var_dump($fruit);  
?>

Running result:

What key names can be used in php arrays

Three assignment methods for index array:

  • array[0]='Apple';

  • $arr=array('0' =>'Apple');

  • ##$arr=array('Apple');

Example:

<?php  
//请创建一个数组变量arr,并尝试创建一个索引数组,键是0,值是苹果  
$arr=array(0=>&#39;苹果&#39;);  
if( isset($arr) ) {print_r($arr);}  
?>

You can use for and foreach to access the elements in the array, because for is easier. Here are just examples of using foreach,

<?php  
$fruit=array(&#39;苹果&#39;,&#39;香蕉&#39;,&#39;菠萝&#39;);  
foreach($fruit as $key=>$value){  
    echo &#39;<br>第&#39;.$key.&#39;值是:&#39;.$value;  
}  
  
?>

Run results:

第0值是:苹果
第1值是:香蕉
第2值是:菠萝

Note: Here $key is the key value and $value is the element value

2. Associative array

"Associative array" is a Arrays with special indexing methods. Index it using a string or other type of value (except NULL).

In fact, the difference between associative arrays and index arrays is only in the key value. The key value of the associative array is a string, and it is an artificial regulation, for example:

<?php  
//创建一个关联数组,关联数组的键“orange”,值是“橘子”  
$fruit=array(&#39;orange&#39;=>&#39;橘子&#39;);  
echo $fruit[&#39;orange&#39;];  
?>

The rest of the initialization, assignment, foreach The usage is basically the same.

Extended knowledge: Convert associative array to index array

In PHP, you can use the array_values() function to convert an associative array to an index array.

array_values() function is to return the values ​​of all elements in the array. It is very simple to use. With only one required parameter, it can return an array containing all the values ​​in the given array, but does not retain the keys. name. The returned array will be in the form of an indexed array, with array indices starting at 0 and increasing by 1.

array_values() function is particularly suitable for arrays with confusing element subscripts, or for converting associative arrays into indexed arrays.

<?php
header("Content-type:text/html;charset=utf-8");
$arr=array(1=>"1","a"=>"",2=>"2","b"=>0,"c"=>"blue");
echo "原关联数组:";
var_dump($arr);
$res=array_values($arr);
echo "转换后的数组:";
var_dump($res);
?>

What key names can be used in php arrays

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of What key names can be used in php arrays. 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