Home > Article > Backend Development > PHP supports several arrays
In PHP, array is a very common and important data type. It can be used to store multiple values that can be accessed based on an index or associated key value. Arrays in PHP support multiple types, which are introduced below.
$numbers = array(1, 2, 3, 4, 5);
The above code creates an indexed array $numbers containing the numbers 1 to 5. Array elements can be accessed by index, for example:
echo $numbers[0]; // Output 1
echo $numbers[2]; // Output 3
$person = array('name' => 'zhangsan', 'age' => 20);
The above code creates an array containing the name and Associative array of ages $person. Array elements can be accessed through key names, for example:
echo $person['name']; // Output zhangsan
echo $person['age']; // Output 20
$students = array(
array('name' => 'zhangsan', 'score' => 95), array('name' => 'lisi', 'score' => 80), array('name' => 'wangwu', 'score' => 70)
);
The above code creates a multi-dimensional array $students that contains information about multiple students. Multidimensional array elements can be accessed by index and key name, for example:
echo $students0; // Output zhangsan
echo $students1; // Output 80
$cars = array(
array('make' => 'Toyota', 'model' => 'Corolla'), array('make' => 'Honda', 'model' => 'Civic')
);
The above code creates an array $cars that contains basic information about multiple cars. Multidimensional array elements can be accessed by index and key name, for example:
echo $cars0; // Output Toyota
echo $cars1; // Output Civic
Summary:
PHP Arrays in support multiple types: indexed arrays, associative arrays, multidimensional arrays, and array composites. Developers can choose the appropriate array type according to specific needs and apply it flexibly.
The above is the detailed content of PHP supports several arrays. For more information, please follow other related articles on the PHP Chinese website!