Home  >  Article  >  Backend Development  >  Is it correct to only have index arrays in php?

Is it correct to only have index arrays in php?

青灯夜游
青灯夜游Original
2022-07-08 18:52:411879browse

There is not only index array in php. PHP not only supports index arrays with numbers as keys, but also supports associative arrays with strings or a mixture of strings and numbers as keys. The subscripts (key names) of an associative array are composed of a mixture of numeric values ​​and strings. As long as one key name in the array is not a number, then the array is an associative array. If the key name is a string, add a delimiting modifier to the key name: single quotation mark "''" or double quotation mark """".

Is it correct to only have index arrays in php?

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

There is not only an index array in php.

In a PHP array, no matter what type of key name there is a value corresponding to it, that is, a key/value pair.

Is it correct to only have index arrays in php?

According to the different data types of array key names, we can divide PHP arrays into two types:

  • Use numbers as keys The name is called an indexed array (Indexed Array);

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

Is it correct to only have index arrays in php?

  • An array with strings or a mixture of strings and numbers as keys is called an Associative Array.

The subscript (key name) of an associative array is composed of a mixture of numeric values ​​and strings. If a key name in an array is not a number, then the array is an associative array. As shown below:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$array=array("id"=>1,"name"=>"李华","age"=>23,"1"=>1,"id2"=>52);
var_dump($array);//打印数组
?>

Is it correct to only have index arrays in php?

The key name of the associative array can be any integer or string. If the key name is a string, add a delimiting modifier to the key name - single quotes ' ' or double quotes " ". For indexed arrays, in order to avoid confusion, it is best to add delimiters.

Note: The key name cannot be NULL.

Extended knowledge: Looping through associative arrays

In PHP, you can use the foreach statement to loop through associative arrays.

foreach is a statement specially designed for traversing arrays. It is a commonly used method when traversing arrays. It provides great convenience in traversing arrays. After PHP5, you can also traverse objects (foreach can only be applied to arrays and object).

The foreach statement traverses the array regardless of the array subscript, and can be used for discontinuous index arrays and associative arrays with strings as subscripts.

  • This statement has two syntax formats:

Syntax format 1:

foreach ($array as $value){
    语句块;
}

Traverse the given $array array , assign the value of the current array to $value in each loop.

  • Syntax format 2:

foreach ($array as $key => $value){
    语句块;
}

Traverse the given $array array, and assign the value of the current array to $value, the key name is assigned to $key.

Description:

When the foreach statement loops, the pointer inside the array will move forward one step, so that the next array element will be obtained in the next loop until the end of the array is traversed. , stop traversing and exit the loop.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of Is it correct to only have index arrays in php?. 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