Is there only index array in php?

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

php not only has index arrays, but also associative arrays. PHP arrays can be divided into two types: 1. Arrays with numbers as keys, that is, index arrays; 2. Arrays with strings or a mixture of strings and numbers as keys, that is, associative arrays. If the key name is a string, use a delimiting modifier (single or double quotes) to wrap the key name.

Is there only index array in php?

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php not only has index arrays, but also There is 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.

Array can store different types of data and is a composite data type. The data structure is as follows:

Is there only index array in php?

In a PHP array, no matter what type of key name there will be a value corresponding to it, that is, a key/value pair, according to Depending on the data type of the array key name, we can divide PHP arrays into two types:

  • Arrays with numbers as key names, that is, indexed arrays (Indexed Array);

  • An array whose key name is a string or a mixture of strings and numbers, that is, an Associative Array.

1) Index array

The subscript (key name) of the index array consists of numbers, starting from 0 by default, and each number corresponds to The position of an array 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;);   
$array=array(1,2,3,4,5,6,7,8,9,10);
echo "第一个元素:".$array[0];
var_dump($array);//打印数组
?>
Copy after login

Is there only index array in php?

#2) Associative array

The subscript (key name) of the 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.

If the key name is a string, use a delimiting modifier for the key name - single quotes '' or double quotes "" pack.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$array=array("id"=>1,"name"=>"李华","age"=>23,"1"=>1,"id2"=>52);
echo "键名为name的元素:".$array["name"];
var_dump($array);//打印数组
?>
Copy after login

Is there only index array in php?

Note: The key name cannot be NULL.

Recommended learning: "PHP Video Tutorial"

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