Home >Backend Development >PHP Problem >What are PHP arrays? How to use PHP arrays?

What are PHP arrays? How to use PHP arrays?

慕斯
慕斯Original
2021-05-26 17:50:232196browse

Do you know what a PHP array is? Can you write something about arrays in PHP? This article will take you through PHP arrays, come and learn together! ! !

What are PHP arrays? How to use PHP arrays?

What is an array:

An array is a special variable that can store multiple values ​​in a single variable.

php array, the original meaning is the array in PHP. Its characteristic is that it maps values ​​to the type of keys. Compared with other languages, the keys of arrays in PHP can be strings, and the values ​​can be of any type.

If you have a list of items (for example: a list of car names), store it into a single variable like this:

$cars1="Lamborghini";
$cars2="Maserati";
$cars3="Maybach";

For an array, if you want to iterate over the array And find out a specific one, if the array has not just 1 item but many, then we should create an array at this time! Because array can store multiple values ​​in a single variable and we can access the values ​​in it based on the key.

To create an array in PHP, the syntax is as follows:

array();

In PHP, there are three types of arrays:

  • Numeric array - an array with numeric ID keys

  • # Associative array - an array with specified keys, each key associated with a value

  • Multidimensional array - an array containing one or more arrays

PHP Numeric Array:

here There are two ways to create a numeric array:

Automatic assignment of ID keys (ID keys always start from 0):

$cars=array("Lamborghini","Maserati","Maybach");

Manual assignment ID key :

$cars[0]="Lamborghini";
$cars[1]="Maserati";
$cars[2]="Maybach";

Specifically, we use code to demonstrate:

The running results are as follows:

What are PHP arrays? How to use PHP arrays?

Get the length of the array - count() function:

count() function is used to return the length of the array (number of elements):

The running results are as follows:

What are PHP arrays? How to use PHP arrays?

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What are PHP arrays? How to use PHP arrays?. 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