What is an indexed array in PHP

青灯夜游
Release: 2023-03-16 16:58:02
Original
3905 people have browsed it

In PHP, an index array refers to an array with numbers as keys (subscripts). 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 is an indexed array in PHP

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

Array is one of the most important data types in PHP First, it is widely used in PHP. Because PHP is a programming language with weak data types, array variables in PHP can store any number of data of any type, and can implement the functions of data structures such as heaps, stacks, and queues in other strong data types.

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

PHP arrays are divided into two types:

  • Those with numbers as key names are called indexed arrays (Indexed Array);

  • Arrays with strings or a mixture of strings and numbers as keys are called associative arrays (Associative Array).

Index array

The subscript (key name) of the index array consists of numbers, starting from 0 by default, and each number corresponds to an array The position of the element in the array does not need to be specified. PHP will automatically assign an integer value to the key name of the index array, and then automatically increase from this value.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$arr = array(&#39;华为&#39;,&#39;三星&#39;,&#39;vivo&#39;,&#39;oppo&#39;);
var_dump($arr);
echo &#39;数组 $arr 中的,键名为2的键值为:&#39;.$arr[2];
?>
Copy after login

What is an indexed array in PHP

Note: If a key name in an array is not a number, then the array is not an index array, but an associative array.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What is an indexed array in PHP. 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