"first element", "second element"...)"."/> "first element", "second element"...)".">

Home  >  Article  >  Backend Development  >  Can PHP array subscripts only start from 0?

Can PHP array subscripts only start from 0?

WBOY
WBOYOriginal
2022-07-22 15:01:363040browse

PHP array subscripts do not only start from 0; the subscripts of arrays in PHP start from 0 by default, but not all start from 0. You can specifically specify the subscript of the array, and PHP will The specified subscript is incremented, and the syntax is "$array = array(specified subscript=>"first element","second element"...)".

Can PHP array subscripts only start from 0?

The operating environment of this article: Windows 10 system, PHP version 8.1, Dell G3 computer

PHP array subscripts do not only start from 0

Numbers in PHP So the subscript of the array starts from 0 by default. There is no need to specify it. PHP will automatically assign it an integer subscript value and then increment it.

Of course, you can also specify the starting subscript. For example:

<?php
$temp_number = array(2=>"one","two","three","four");
print_r($temp_number);
?>

Output result:

Can PHP array subscripts only start from 0?

In this way, "one" is saved at the key value 2, 3, 4, and 5 respectively. , "two", "three", "four" four element values.

We can also arbitrarily specify the key name of the array to save the array value, for example:

$temp_number = array(2=>"one",4=>"two",6=>"three");

Output result:

Can PHP array subscripts only start from 0?

In this way, in the key name The positions of values ​​2, 4, and 6 store the values ​​of the three array elements "one", "two", and "three" respectively.

Expand knowledge

Array basics:

In php, the subscript of an array can be an integer or It is a string

In php, the order of the elements of the array is not determined by the subscript, but by the order of their "joining"

Definition:

$arr1 = array(元素1,元素2,。。。。。。);
array(1,1.1,5,&#39;abc&#39;,true,false); //可以存储任何数据,此时为默认下标
array(2=>1,4=>1.1,3=>5,7=>&#39;abc&#39;,0=>true); //下标可任意设定(无需顺序,无需连续)
array(2=>1,1.1,1=>5,&#39;abc&#39;,0=>true); //可以加下标,也可以不加,不加下标则为默认下标
//默认下标规则:前面已经用过的最大数字下标+1
//这个数字的下标分别是:2,3,1,4,0
array(2=>1,&#39;dd&#39;=>5,1=>1.1,&#39;abc&#39;,0=>true); //混合下标,同样遵循默认下标规则
array(-2=>1,&#39;dd&#39;=>5,1.1,&#39;abc&#39;,true); //负数下标不算在整数下标中,而只当作字符下标
//则这个数组最后三项的下标为:0,1,2
array(2.7=>1,&#39;dd&#39;=>5,1=>1.1,&#39;abc&#39;,true); //浮点数下标会自动转换为整数,且直接去掉小数部分
array("2.7"=>1,&#39;dd&#39;=>5,"11"=>1.1,&#39;abc&#39;,true); //纯数字字符串下标,当作数字看待
array(2=>1,&#39;dd&#39;=>5,true=>1.1,&#39;abc&#39;,false=>true); //布尔值当作下标,则true为1,false为0
array(2=>1,&#39;dd&#39;=>5,2=>1.1,&#39;abc&#39;,true); //如果下标跟前面的重复,则单纯覆盖前面同名下标的值

Other forms:

$arr1[] = 1;
$arr1[] = 5;
$arr1[] = 1.1;
......  //直接在变量后面使用[],就成为数组,并依次赋值
$arr2[&#39;aa&#39;] = 1;
$arr2[&#39;bb&#39;] = 5;
$arr2[5] = 1.1;
......  //这种形式写的下标,其实跟使用array语法结构几乎一样

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of Can PHP array subscripts only start from 0?. 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
Previous article:Can php split an array?Next article:Can php split an array?