How to determine the type of PHP array

WBOY
Release: 2023-03-13 09:46:01
Original
2456 people have browsed it

In the previous article, I brought you "How to use the switch statement in PHP (example analysis)". This article introduces the basic content and detailed examples of the PHP switch statement in detail. , today we continue to look at arrays, one of the most important data types in PHP. I hope everyone has to help!

How to determine the type of PHP array

PHP Array

What is an array?

One of the most important data types in PHP is array, which is widely used in PHP. Arrays can store multiple different types of data and are a composite data type. An array is a special variable that can store multiple values in a single variable.

If you want to achieve batch data processing in a collection unit, you need to use an array to organize multiple interrelated data together to form a collection.

How to create an array?

In PHP, the array() function is used to create an array. Each array element is separated by commas, for example:

Copy after login

In the above example we are in the array Added: string, integer, Boolean, floating point. There can be many elements in the array. To find it, we need to use two concepts: "key (subscript)" and "value (value)". The corresponding array element can be obtained by key value.

The subscript means that the key usually starts from 0, 0.1.2.3...that is:

Copy after login

Output result:

How to determine the type of PHP array

The subscript usually starts from 0, so can it not start from zero? Then you need to use one of the types of arrays called: index array.

Index array

The subscript of the index array, that is, the key name, is composed of numbers. Generally, it starts from 0 by default. Each number corresponds to an array element. Position in the array, PHP will automatically assign an integer value to the key name of the index array, and then automatically increase from this value. As shown below:

'好好学习','天天向上',5 => '福如东海','寿比南山', 10 => 'nothing'); ?>
Copy after login

0=>'Study hard'; means: the subscript of study hard is 0, "Every day up" is behind "study hard", and its subscript is automatically incremented. That’s 2.

where => is a symbol, the standard name is: key-value corresponding symbol. It can be understood as a subscript that declares this value.

Therefore, the subscript of "福如东海" is declared to be 5, and the subscript of "Shoubi Nanshan" will increase after 5, which is 6.

An example is as follows:

"好好学习","天天向上",6=>"福如东海","寿比南山"); echo $a[6]; echo $a[7]; echo $a[2]; echo $a[3]; ?>
Copy after login

Output result:

How to determine the type of PHP array

Through the above example we can summarize the rules of index arrays:

  • If the index array does not forcefully declare its subscript, its subscript starts from 0.

  • The subscript of the specified subscript is the value I specified.

  • A certain value has a mandatory subscript. The value after it, if no subscript is specified. Their subscripts are incremented by 1.

Associative array

The subscript of an associative array is composed of a mixture of numeric values and strings. If there is a key in an array If the name is not a number, then the array is an associative array. For example:

"10086","city"=>"12315","boy"=>"12306"); echo $a['young'] . $a['city'] . $a['boy']; ?>
Copy after login

Output result:

How to determine the type of PHP array

The subscript of an associative array can be an integer or a string. If the subscript is a string, add single quotes' 'or double quotes" "to the subscript.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to determine the type of PHP array. 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
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!