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 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:
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:
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!