This article mainly introduces PHP's definition of arrays and the method for creating arrays. PHP has arrays and multi-dimensional arrays like traditional compiled languages, for more information, see the traditional definition of arrays as a group of elements with some common features. the common features here include similarity (car model, baseball team, fruit type, etc) and type (for example, all elements are strings or integers). each element is distinguished by a special identifier, which is called a key ). Note that the traditional term in the above sentence can discard this definition, and the array structure can contain completely unrelated elements. PHP goes further, and the elements in the array may not even belong to the same type. For example, an array may contain state names, zip codes, test scores, poker cards, and other elements.
Each entity contains two items: the key and value mentioned above ). You can use the query key to obtain the corresponding value. These keys can be numeric keys or associative keys. There is no real relationship between the value key and the value, they are only the position in the array. For example, an array contains fruit names in alphabetical order. The key 0 indicates apple, and the key 2 indicates pear. Use PHP syntax. the array is as follows:
$fruits = array( "0"=>"apple", "1"=>"banana" "2"=>"pear" );
Using array indexes, you can reference the first element (apple) as follows ):
$fruits[0]
The value index group of PHP starts with position 0, instead of 1.
The difference is that the correlated key has a certain relationship with the value, rather than the position in the array. When numeric index values are not feasible, it is very convenient to map arrays in an association manner. For example, you may want to create an array that maps fruit abbreviations to fruit names, such as AP/apple, BA/banana, and PE/Pear. Use PHP syntax. the array is as follows:
$fruits = array( "AP"=>"apple", "BA"=>"banana", "PE"=>"pear" );
You can reference apple as follows:
$fruits["AP"];
You can also create an array containing an array, which is called a multi-dimensional array (multidimenstmarrays ). For example, you can use a multi-dimensional array to store fruit information. Use PHP syntax. the array is as follows:
$fruits = array( "apple"=>array( "name"=>"apple", "color"=>"red" ), "banana"=>array( "name"=>"banana", "color"=>"yellow" ));
Then, you can reference the color of apple as follows:
$states["apple"]["color"];
This will return the following values:
red
You will naturally want to know how to traverse the array. PHP provides many methods to traverse arrays. Whatever method you use, remember that they all depend on a feature called array pointer. The array pointer is like a bookmarklet, indicating the position of the array being checked. Instead of directly operating the array pointer, you use the built-in language features or functions to traverse the array. However, it is useful to understand this basic concept.
Arrays are one of the most important data structures in PHP. arrays are widely used in PHP. Unlike the array implementation method in many other languages, PHP does not need to specify its size when creating an array. In fact, because PHP is a loose language, you do not even need to declare it before using arrays. although there are no restrictions, PHP still provides formal and informal array declaration methods. Both methods have their own advantages and are worth learning. The following two methods are discussed respectively. First, we will introduce informal methods.
To reference each element in the PHP array, you can use a pair of brackets to indicate. Because there is no size limit on the array, you only need to create a reference to create an array, for example:
$fruits[0] = "apple";
Then, the first element of the array $ fruits is displayed as follows:
echo $fruits[0] = "apple";
Next, you can map new values to the array index to add other values, as shown below:
$fruits[1] = "banana";$fruits[2] = "pear";
Interestingly, if you think that the index value is an array index and increases progressively, you can also omit the index value during creation:
$fruits[] = "apple";$fruits[] = "banana";$fruits[] = "pear";
Using this method to create an associated array is also very simple, but you must always use the key. The following example creates an array that maps fruit to its color:
$fruits["apple"] = "red";$fruits["banana"] = "yellow";$fruits["pear"] = "yellow";
Use array () to create an array
The array () function accepts 0 or more elements as input and returns an array containing these elements. The format is as follows:
array array([item1,[,item2…[,itemN]]])
The following is an example of creating an index array using array:
$fruits = array("apple","banana","pear");
You can also use array () to create an associated array, as shown below:
$fruits = array( "AP"=>"apple", "BA"=>"banana", "PE"=>"pear" );