Home>Article>Backend Development> What are the two types of php arrays?
What is an array?
Array is a special variable that can save more than one value at the same time. (Recommended learning:PHP Programming from Beginner to Master)
If you have a list of items (such as a list of car brands), storing these brand names in a single variable is like this:
$cars1="porsche"; $cars2="BMW"; $cars3="Volvo";
However, what if you want to iterate through the variables and find a specific value? Or what if you need to store 300 car makes instead of 3?
The solution is to create an array!
Arrays can store many values in a single variable name, and you can access a value by referencing an index number.
PHP Index Array
There are two ways to create an index array:
The index is automatically assigned (index starts from 0):
$cars=array("porsche","BMW","Volvo");
PHP Associative Arrays
An associative array is an array using specified keys that you assign to the array.
There are two ways to create associative arrays:
$age=array("Bill"=>"35","Steve"=>"37","Elon"=>"43");
The above is the detailed content of What are the two types of php arrays?. For more information, please follow other related articles on the PHP Chinese website!