Arrays in PHP are very important data structures that can store and process various types of data, including numbers, strings, Boolean values, objects, etc. The key value is the label used to identify each element in the array. It gives the array element a unique identity so that the corresponding value can be accessed through the key.
1. What is a PHP array
In PHP, an array is a composite data structure that can be used to store multiple values, and these values can be of different types. The way to define arrays in PHP is very flexible, including the following ways:
1. Define the array through the array() function
$array = array('a', 'b', ' c');
2. Use square brackets [] to define the array
$array = ['a', 'b', 'c'];
3. Mix the array() function and square brackets to define an array
$array = array('a', 'b', 'c', 1=> 'd', 2=> 'e', 'f' => 'g');
2. What are key values of PHP arrays
In PHP, key values are labels used to identify array elements. In an array, each element has a unique key value, and the value of the corresponding array element can be accessed through the key value.
The key values in the array can be numbers, strings, and enumeration types, but they must be unique. If two elements have the same key value, only the last element will be retained. The key value can be of any type, but if the key value is a string, it can be enclosed in quotes.
In PHP, there are two main types of key values, namely index arrays and associative arrays.
1. Index array
The index array is the most commonly used array type. Its key value is an integer, usually starting from 0 and increasing. For example:
$array = array('apple', 'banana', 'orange');
In this array, the key value of 'apple' is 0, the key value of 'banana' is 1, and the key value of 'orange' is 2.
You can use square brackets or array functions to access, modify, or delete elements of the indexed array. For example:
$array[0] = 'pear'; unset($array[1]); print_r($array); // 输出结果为:Array([0] => 'pear' [2] => 'orange')
Note that when we delete an element, the key values in the array will not be reordered. Reordering can only be achieved by using the array_values() function to renumber the array elements after rearranging the key values.
2. Associative array
Associative array refers to an array that accesses array elements by specifying a custom key. Typically, associative array keys are of type string. For example:
$user = array('name' => 'Tom', 'age' => 18, 'gender' => 'male');
In this array, 'name', 'age', and 'gender' are all key values, and their key values are 'Tom', 18, and 'male' respectively.
Similarly, we can use square brackets or array functions to access, modify or delete elements of an associative array:
$user['name'] = 'Jerry'; unset($user['age']); print_r($user); // 输出结果为:Array(['name'] => 'Jerry' ['gender'] => 'male')
It should be noted that in an associative array, the key value does not have to be an integer Appears in incremental form, so we can define the key value arbitrarily as needed. This is why associative arrays are more flexible than indexed arrays.
3. Supplementary explanation
PHP’s arrays also have some other features, such as:
1. Multidimensional arrays
Multidimensional arrays are indexed in arrays Contains other arrays, which means it is a collection of arrays. In PHP, there is no limit to the dimensions of multidimensional arrays, which means there can be any number of levels. For example:
$fruit = array( 'apple' => array('color' => 'red', 'price' => 5.5), 'banana' => array('color' => 'yellow', 'price' => 3.2), 'orange' => array('color' => 'orange', 'price' => 2.8) );
In this multi-dimensional array, each element is an associative array, and the corresponding value can be accessed through the key value.
echo $fruit['apple']['color']; // 输出结果为:red
2. Array traversal
We can use for loop, foreach loop or while loop to traverse the array to access each element. For example:
Use for loop
$fruit = array('apple', 'banana', 'orange'); for($i = 0; $i < count($fruit); $i++) { echo $fruit[$i]; }
Use foreach loop
$fruit = array('apple', 'banana', 'orange'); foreach($fruit as $value) { echo $value; }
Use while loop
$fruit = array('apple', 'banana', 'orange'); $i = 0; while($i < count($fruit)) { echo $fruit[$i]; $i++; }
Summary:
Arrays in PHP It is a very important data structure that can handle various types of data and provides flexible definition and use. The key value is a unique identifier used to identify each element in the array. It can be an integer, string, or enumeration type. The elements in the array can be accessed, modified, and deleted through the key value, and the array can also be sorted by changing the key value. For multidimensional arrays, we can use multiple key values to index each element. For array traversal, we can use methods such as for loop, foreach loop or while loop to access each element.
The above is the detailed content of What is the key value of php array. For more information, please follow other related articles on the PHP Chinese website!