Home > Article > Backend Development > What is the function to define array in php
An array in PHP is actually an ordered map. A mapping is a type that associates values to keys. This type is optimized in many ways, so it can be treated as a real array, or list (vector), hash table (which is an implementation of map), dictionary, set, stack, queue and many more possibilities.
Since the value of an array element can also be another array, tree structures and multidimensional arrays are also allowed.
Define array array() (Recommended learning: PHP video tutorial)
You can use array() language structure to create a new array. It accepts any number of comma-separated key => value pairs.
In PHP, there are three types of arrays:
Indexed array - array with numeric index
Associative array - with Array with specified key
Multidimensional array - Array containing one or more arrays
Description
array() creates an array with keys and values. If the key is omitted when specifying the array, an integer key is generated that starts at 0 and increments by 1.
To create an associative array with array(), use => to separate keys and values.
To create an empty array, do not pass parameters to array():
$new = array();
Note: array() is actually a language construct, usually used to define direct array, but its usage is very similar to that of functions.
Example
Create an indexed array named $cars, assign three elements to it, and then print the text containing the array value:
<?php $cars=array("Volvo","BMW","Toyota"); echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . "."; ?>
The above is the detailed content of What is the function to define array in php. For more information, please follow other related articles on the PHP Chinese website!