Home  >  Article  >  Backend Development  >  Basics of array application in PHP (3)

Basics of array application in PHP (3)

黄舟
黄舟Original
2016-12-22 10:37:501092browse

The more important basic syntax of PHP has been given out. After mastering these, in fact, my teacher said that the most important thing in PHP is the array. It can be seen everywhere. As the programming increases, I feel this way more and more.

1. Definition of array

Array: It is a collection of data that organizes a series of data to form an operable whole. It includes elements and subscripts. The element is an array that can store multiple values, and each value is called an element of the array; the subscript means that each array element has a related index, which can be regarded as the identification name of the data content in the array, and is usually also called Called the array subscript or key name.

2. Classification of arrays

Basically there are these: enumeration (index) arrays and associative arrays, which are divided according to the type of the index value. According to the dimensions of the array, it can be divided into one-dimensional array, two-dimensional array and multi-dimensional array. Commonly used are one-dimensional arrays and two-dimensional arrays. Above code:
//Declare the enumeration array
$meiju_arr = array("Pig Head","www.wozhutou.com","E-commerce","Male","Web Engineer");
print_r($suoyin_arr); //Use print_r to print the structural information of the array
//The following is the declaration of the associative array
$guanlian_arr = array(
"username" => "Pig Head",
"web" => "www.wozhutou.com",
"hangye" => "e-commerce",
"sex" => "male",
"position" => "Web engineer"
);
print_r($guanlian_arr); //Use print_r to print the array Structural information

Associative arrays and enumeration arrays are easier to identify. The default subscript is an enumeration, and the custom subscript is an association.

//The above are all one-dimensional arrays, and the following are two-dimensional enumeration arrays
$erwei_arr = array(
array("Pig Head","www.wozhutou.com","E-commerce","Male","Web Engineer"),
array("Zhutou Blog","wozhutou.com","E-commerce Operation Platform","Male","Web Engineer"),
);

//The following is a two-dimensional associative array
$ erwei_arr = array(
"A" => array("Pig Head","www.wozhutou.com","E-commerce","Male","Web Engineer"),
"B" => array(" **","********","E-commerce Operation Platform","Male","Web Engineer"),
);
3. Creation and initialization of arrays

Create two elements of the array Types: 1. Use the array() function to declare an array; 2. Directly assign values ​​to array elements

The code for using the array() function to declare the second point of an array has already been used. Let’s look at the code for direct assignment:
$my_arr["username "] = "anzai";
$my_arr["realname"] = "Anzai";
$my_arr["sex"] = "Male";
$my_arr["age"] = "22″;
$my_arr ["position"] = "Web Engineer";

4. Accessing arrays

We generally access the value of the corresponding element through a subscript or assign a value to the corresponding element (especially point out that the array element assignment symbol is used when assigning values ​​​​above) =>”). Write the access code directly here, and access it according to the above assignment code:

echo "Industry:" . $meiju_arr [2]; //This is the access enumeration array. Outputs the value of the array element with index 2, and the result is e-commerce.

echo "Industry:" . $meiju_arr [hangye]; //This is to access the associative array. Outputs the value of the array element with index hangye, and the result is e-commerce.

The above is the content of PHP’s array application basics (3). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


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
Previous article:PHP data type string typeNext article:PHP data type string type