Home >Backend Development >PHP Problem >PHP only has index arrays, right?

PHP only has index arrays, right?

青灯夜游
青灯夜游Original
2022-07-25 17:34:462187browse

wrong. 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 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, and does not need to be specified. The subscripts (key names) of associative arrays are 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.

PHP only has index arrays, right?

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

is incorrect. There are not only index arrays in php, but also associative arrays.

Array An array is an ordered set of variables in which 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 only has index arrays, right?

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

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

  • An array in which strings or strings and numbers are mixed as keys is called 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
    $arr = array(&#39;华为&#39;,&#39;三星&#39;,&#39;vivo&#39;,&#39;oppo&#39;);
    print_r($arr);
    echo &#39;<br/><br/><br/>&#39;;
    echo &#39;数组 $arr 中的,键名为2的键值为:&#39;.$arr[2];
?>

PHP only has index arrays, right?

can also be written like this:

<?php
    $arr1 = array(&#39;0&#39; => &#39;苹果&#39;,&#39;1&#39; => &#39;香蕉&#39;,&#39;2&#39; => &#39;橘子&#39;,&#39;3&#39; => &#39;李子&#39;,&#39;4&#39; => &#39;草莓&#39;);
    print_r($arr1);
    echo &#39;<br/><br/>&#39;;
    $arr2[0] = &#39;苹果&#39;;
    $arr2[1] = &#39;香蕉&#39;;
    $arr2[2] = &#39;橘子&#39;;
    $arr2[3] = &#39;李子&#39;;
    $arr2[4] = &#39;草莓&#39;;
    print_r($arr2);
?>

PHP only has index arrays, right?

2) 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.

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.

<?php
    $arr1 = array(&#39;Apple&#39; => &#39;苹果&#39;,&#39;Banana&#39; => &#39;香蕉&#39;,&#39;Orange&#39; => &#39;橘子&#39;,&#39;Plum&#39; => &#39;李子&#39;,&#39;Strawberry&#39; => &#39;草莓&#39;);
    print_r($arr1);
?>

PHP only has index arrays, right?

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of PHP only has index arrays, right?. 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