Home >Backend Development >PHP Problem >There are several types of php array subscripts

There are several types of php array subscripts

PHPz
PHPzOriginal
2023-04-26 14:22:22969browse

In PHP programming, array is a very important data type. When processing data, arrays can help us easily store, search, and sort data. The subscript in the array is a very important concept. Below we will introduce in detail the different types of PHP array subscripts.

1. Numeric subscript array

Numeric subscript array is the most basic array type in PHP. Its subscript is a series of integers starting from 0. This type of array is very common because most arrays store and access their elements by numerical subscript. For example:

$arr = array("apple", "orange", "banana");
echo $arr[0]; //输出"apple"
echo $arr[1]; //输出"orange"
echo $arr[2]; //输出"banana"

2. Associative array

Associative array refers to a set of strings whose subscripts are defined by the user. This type of array is very suitable for storing some strings that need to be The data type used as an index, such as dictionary, configuration file, etc. For example:

$info = array("name"=>"Tom", "age"=>18, "gender"=>"male");
echo $info["name"];    // 输出"Tom"
echo $info["age"];     // 输出18
echo $info["gender"];  // 输出"male"

In this example, we can see that the subscript of the associative array is a string defined by the user, rather than automatically generated by the system like the numeric subscript array.

3. Multidimensional Array

A multidimensional array is an array that contains another or multiple arrays within an array. It can also be said to be an array of arrays. This type of array can be nested in multiple levels, but its basic principle is the same as the previous two types of arrays. As long as you master the previous two types of arrays, you can easily handle multi-dimensional arrays. For example:

$goods = array(
    array("name"=>"apple", "price"=>10),
    array("name"=>"orange", "price"=>8),
    array("name"=>"banana", "price"=>12)
);

echo $goods[0]["name"];    // 输出"apple"
echo $goods[1]["price"];   // 输出8
echo $goods[2]["name"];    // 输出"banana"

In this example, the $goods array contains three elements. Each element is an associative array. This associative array contains the two elements of the product name and price. Through multiple In one visit, we can get the specific data nested in the array.

4. Empty subscript array

Empty subscript array refers to an array whose subscripts are not set correctly. This situation is often caused by the programmer making an error when writing the code. The array subscript was set to an incorrect value, or an empty array was accidentally created. For example:

$arr = array("apple", "orange", "banana");
$arr[] = "grape";
echo $arr[3];    // 输出"grape"
$arr[5] = "peach";
echo $arr[4];    // 输出null
echo $arr[5];    // 输出"peach"

In this example, we can see that a new element "grape" is added at the end of the array, but since we did not specify the subscript of the new element, the system will automatically generate an $arr subscript of [3]. When we assign $arr[5], because element No. 4 is missing in the middle of the array, the output result is null.

5. Negative subscripts in arrays

In addition to normal number and string subscripts, PHP arrays also support the use of negative subscripts to access elements. When using a negative subscript, the array will automatically add the subscript value to the length of the array to achieve the purpose of accessing the nth element from the last. For example:

$arr = array("apple", "orange", "banana");
echo $arr[-1];  // 输出"banana"
echo $arr[-2];  // 输出"orange"
echo $arr[-3];  // 输出"apple"

In this example, we can see that when using negative subscripts, the array does not cause an error, but returns the value of the nth element from the last we want.

Summary

PHP array subscripts are divided into numeric subscript arrays, associative arrays, multi-dimensional arrays, empty subscript arrays and negative subscripts in arrays. Each type of subscript has different applications in different scenarios. Programmers need to choose the most appropriate subscript type based on different needs when processing arrays.

The above is the detailed content of There are several types of php array subscripts. 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