Home  >  Article  >  Backend Development  >  PHP supports several arrays

PHP supports several arrays

WBOY
WBOYOriginal
2023-05-22 19:33:06435browse

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.

  1. Index array
    Index array is the most basic array type in PHP. It stores and accesses values ​​according to numerical index. The first element in the index array has an index value of 0, and then increases sequentially. An indexed array can be created using the array() function. For example:

$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

  1. Association Array
    Associative array is another common array type. It uses key-value pairs to store and access values. Each key is equivalent to an index in the array and is used to access and store values. An associative array can be created using the array() function. For example:

$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

  1. Multidimensional array
    A multidimensional array is an array composed of multiple nested arrays. These nested arrays can be index arrays, associative arrays, or even other multidimensional arrays. Multidimensional arrays make it easier to organize and represent data. For example:

$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

  1. Array composite
    PHP's arrays can be used to create more complex data structures in code. Array composition provides a way to create array compositions that use comma-separated lists to put keys and values ​​together. For example:

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

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