The function library in the array is the main point for us to learn about arrays. Here we will mainly introduce the PHP array function library array to create an array. Let's share it with everyone. Fortunately, PHP is not difficult, and there are not many such things. After learning a few more lessons, we can start to use it to implement the functions of a website. I believe that you will become more and more energetic. Okay. Start today's course. Today, we are studying arrays. arrays are a type of data that is frequently used. Learning to process arrays will make your website easy.
For example, your website database contains a large article. Now you want to display 20 titles of entertainment news on a page, do you want to use 20 variables after these 20 titles are extracted from the database? I think you will not be so stupid, so what variables can be used to fully receive the 20 pieces of data? This requires array variables. What is an array? An array is actually a data set, which is equivalent to a data container. A lot of data is stored in it. We can store or retrieve it in a certain way, you can also sort the data in it, and check whether there is any data we want.
PHP array function library new array use array
- array array ( [mixed ...] )
Returns an array created based on parameters. You can use the => operator to provide the index for the parameter. For information about what an array is, read the array section. Note: array () is a language structure used to represent arrays literally, rather than regular functions. The syntax "index => values" is separated by commas (,), which defines the index and value. The index can be a string or number. If the index is omitted, an integer index starting from 0 is automatically generated. If the index is an integer, the next index will be the currently Largest Integer index + 1. Note that if two identical indexes are defined, the next one will overwrite the previous one. Although it is uncommon to add a comma after the last defined array item, it is a legal syntax.
The following example demonstrates how to create a two-dimensional array, how to specify a key name for the corresponding array, and how to slightly over and continue the Numerical index in a common array.
PHP array function library example 1. array () Example
- <?php
- $fruits = array (
- "fruits" => array("a" => "orange", "b" => "banana", "c" => "apple"),
- "numbers" => array(1, 2, 3, 4, 5, 6),
- "holes" => array("first", 5 => "second", "third")
- );
- ?>
PHP array function library example 2. Automatic Index of array ()
- <?php
- $arrayarray = array(1, 1, 1, 1, 1, 8 => 1, 4 => 1, 19, 3 => 13);
- print_r($array);
- ?>
The above example will output:
- Array
- (
- [0] => 1
- [1] => 1
- [2] => 1
- [3] => 13
- [4] => 1
- [8] => 1
- [9] => 19
- )
Note that index 3 is defined twice and the last value is 13. Index 4 is defined after index 8. The next automatically generated index (the value is 19) is 9, because the maximum index is 8.