In PHP, array is a very important data type. It can store multiple values and access them through array keys. PHP supports multiple types of arrays, including indexed arrays, associative arrays, and multidimensional arrays. This article will introduce several common array types in PHP and how to operate these arrays.
1. Index array
The index array is the most basic array type in PHP. It uses integers such as 0, 1, 2... as keys, and each key corresponds to a value. In PHP, you can create an index array in the following way:
$arr1 = array('apple', 'banana', 'orange');
Or use the following concise way:
$arr1 = ['apple', 'banana', 'orange'];
To access the elements in the index array, you can get the corresponding value through the array key, For example:
echo $arr1[0]; // 输出 apple echo $arr1[1]; // 输出 banana echo $arr1[2]; // 输出 orange
You can also use a loop to traverse the entire array:
foreach ($arr1 as $value) { echo $value . ' '; } // 输出 apple banana orange
In addition to the above methods, you can also use a series of array functions provided in PHP to operate the index array, such as the sort() function Arrange the array in ascending order:
sort($arr1); print_r($arr1); // 输出 Array ( [0] => apple [1] => banana [2] => orange )
2. Associative array
The associative array is an upgrade based on the index array. It uses strings as key names to store values. Associative arrays provide a more flexible way to access array elements. For example:
$arr2 = array('name' => 'John', 'age' => 30, 'country' => 'USA');
Or concisely written:
$arr2 = ['name' => 'John', 'age' => 30, 'country' => 'USA'];
To access the elements in the associative array, you can get the value through the key name:
echo $arr2['name']; // 输出 John echo $arr2['age']; // 输出 30 echo $arr2['country']; // 输出 USA
Similarly, you can also use the foreach loop Traversing associative arrays:
foreach ($arr2 as $key => $value) { echo $key . ':' . $value . ' '; } // 输出 name:John age:30 country:USA
Associative arrays also provide some special operation functions. For example, the ksort() function can arrange the array in ascending order according to the key name:
ksort($arr2); print_r($arr2); // 输出 Array ( [age] => 30 [country] => USA [name] => John )
3. Multidimensional arrays
A multidimensional array is an array composed of multiple arrays (including index arrays and associative arrays). Usually called two-dimensional array, three-dimensional array, etc. In PHP, you can create a two-dimensional array using the following method:
$arr3 = [ ['name' => 'John', 'age' => 30, 'country' => 'USA'], ['name' => 'Alice', 'age' => 25, 'country' => 'UK'], ['name' => 'Bob', 'age' => 35, 'country' => 'Canada'] ];
To access elements in a multidimensional array, you need to use multiple keys to get the corresponding values:
echo $arr3[0]['name']; // 输出 John echo $arr3[1]['age']; // 输出 25 echo $arr3[2]['country']; // 输出 Canada
When using multidimensional When using an array, you can also traverse the entire array through a foreach loop:
foreach ($arr3 as $items) { foreach ($items as $key => $value) { echo $key . ':' . $value . ' '; } echo '<br>'; }
The above code will output all elements in the entire two-dimensional array.
4. Summary
The array types in PHP are very rich, including index arrays, associative arrays and multi-dimensional arrays, etc. Different types of arrays provide different ways to store and access array elements. You can choose the appropriate array type according to actual needs. At the same time, PHP also provides a series of array functions to operate arrays, and developers can choose the appropriate function according to actual needs.
The above is the detailed content of Let's talk about several array types in php. For more information, please follow other related articles on the PHP Chinese website!