Can keys in php arrays be numbers?

青灯夜游
Release: 2023-03-16 14:46:02
Original
2201 people have browsed it

The keys of the php array can be numbers. In PHP, index arrays with numbers as key names are supported; 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, and does not need to be specifically specified. PHP will automatically assign an integer value to the key name of the index array, and then automatically increase from this value.

Can keys in php arrays be numbers?

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

The keys of the php array can be numbers .

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).

PHP arrays are more flexible than arrays in other high-level languages. Not only does support index arrays with numbers as keys, but also supports strings or a mixture of strings and numbers as keys. Associative array.

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.

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

Can keys in php arrays be numbers?

Example 2:

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

Can keys in php arrays be numbers?

##Expand knowledge: 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);
?>
Copy after login

Can keys in php arrays be numbers?

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of Can keys in php arrays 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!