php array definition
Array Before, we let everyone remember two points:
1. Arrays can store multiple different types of data and are a composite data type.
2. The English name for array is array. Let’s learn the simplest array declaration.
Then let’s do a brief review:
'; var_dump($shu); echo ''; ?>
In the above example, we found that we deposited:
1. Integer type
2 .Floating point
3.Boolean
4.String
Note: The main purpose of the echo pre tag in the above example is to output it as it is, and the format will be displayed better ,clearer.
We use var_dump() to print it out and see the effect:
If we look carefully at the picture above, you will find the following features: :
1.array(size = 4) It means there are 4 elements in it
2.0 => int 1 We know that int means integer, and 1 is an integer value. So what do the previous 0, 1, 2, 3 and => mean?
3. The latest 0, 1, 2, and 3 represent the reading identification number of the value, which we call the subscript or key (English: key)
4.= > is a standard name for a symbol called: key-value correspondent. Therefore, later seeing 0=> int 1 can be said like this. The subscript accessor 0 corresponds to 1 of the integer type.
5. We also call the key-value pairs in the array elements, and elements are combinations of key-value pairs.
Oh yeah! Arrays seem to be quite easy to learn, as there are some rules.
Through the above example, you actually accidentally completed one of the declaration methods of an array: the declaration of an index array.
The so-called index array: it is an array whose subscripts are all integers.
Does the subscript of the index array have to start from 0?
Answer: This question is actually not true. The index array does not necessarily start from 0.
How can we not start from 0?
Answer: You need to use a small piece of knowledge you learned above. It’s the key-value correspondent. Let's start writing.
'NoAlike', 'PHP中文网' , '去PHP中文网学PHP', 19 => '凤姐和芙蓉我都爱' , '杨幂我最爱'); //打印显示$kele echo ''; var_dump($kele); echo ''; ?>
In the above example, we accidentally wrote an index array. However, the subscripts of the index array do not start from 0, but from 10.
However, in the above example, we feel that the writing is not beautiful. We can write the code more beautifully and the format is clearer.
'NoAlike', 'PHP中文网' , '去PHP中文网学PHP', 19 => '凤姐和芙蓉我都爱' , '杨幂我最爱' ); //打印显示$kele echo ''; var_dump($kele); echo ''; ?>
Does this make it clearer? One row corresponds to the value of an array.
Let’s execute the code and see the effect:
Through the above effect, let’s summarize the rules:
1. If the index array does not forcefully declare its subscript, its subscript starts from 0. (The value of our first array: Only through continuous efforts can we win the future. The subscript of this value is 0).
2. If I have specified a subscript, his subscript will be the value I specified. For example, the subscript 10 and the subscript 19 are the values I have specified.
3. If a value (such as NoAlike) is forced to specify a subscript (the subscript is 10). The value added after it (PHP Chinese website), if the subscript is not specified. Their subscript growth pattern is maximum +1.
For example: I love both Sister Feng and Furong, the subscript is 19. I added at the end: Yang Mi is my favorite. Its subscript automatically increases to 11.
Unknowingly, you have learned how to create an index array, isn’t it amazing? So happy for you!
Add elements to the index array
After learning the creation of the index array, next we learn theadding and modifying the index array and delete.
Summary:
1. To add elements to the index array, use two methods: array variable name [], array variable name [key value] to add elements
2. The growth rules of key values are the same as the previous rules. They are all based on the principle of adding 1 to the maximum value.
Delete elements from the index array
Let’s take the array just now as an example:
'; var_dump($minren); echo ''; ?>
Look at the effect:
1. Use unset to delete the variable to delete the value in the array.
2. Deleting the middle value will not automatically move the subsequent subscripts forward. But it is whatever the original value is.
3. Delete one of the values. The newly added value will not replace the original position, and still follow the principle of adding 1 to the maximum value.
Modify value
We learn the simplest creation, addition and deletion. I believe everyone will be able to reason out how to modify the value.
'; var_dump($minren); echo ''; ?>
Execute the above code and output it to see the result.
1. Use variable name [key] = new value. The values in the array are modified.
Other ways to declare index arrays
Through the above example, we learned about the declaration of arrays. Let's learn other ways to declare arrays.
1. Directly use previously undeclared variables and declare the array by following the variable name with brackets.
'; var_dump($qi); echo ''; ?>
2. It’s too troublesome to write using array() every time. You don’t need to write array, it’s simpler.
'刘亦菲', '黄圣依', '范冰冰' ]; echo ''; var_dump($minren); echo ''; ?>
The above are two other ways of writing. Of course, you can use whichever one you like.
Associative array
The index array is appropriately transformed and an associative array appears. As long as there is an array of strings in the array, it is an associative array.
Through the above example, we found that the subscript of the array can only be a string, which of course cannot meet my needs.
Suppose I want to declare a subscript as handsome corresponding to the value of Eason Chan. It certainly doesn't meet my needs.
Then let’s declare the associative array. This is done in the same way as the declaration of the indexed array. But the difference is that the subscript of the string must be specified and the key-value correspondence must be used.
'陈奕迅', '很帅' => '黄晓明', '灰常灰常帅' => '宁泽涛', '有男人味的大叔' => '吴秀波', ); //再来玩玩简洁声明 $drink = [ '美' => '凤姐', '很美' => '芙蓉姐姐', 'verymei' => '杨幂', '心中滴女神呀' => '华妃', 100 => '孙俪', '娘娘', ]; // 输出 $rela echo ''; var_dump($rela); echo ''; // 输出$drink echo ''; var_dump($drink); echo ''; ?>
Let’s experiment to see what the final result is:
We know through experiments:
1. Statement An associative array is a key name => value
2. An associative array can have elements of the index array
3. An element without a subscript is declared after the element of the index array in the associative array , it is still the maximum +1 principle. (Observe that the values in the above picture are the two elements of Sun Li and Empress).
Adding, deleting, and modifying associative arrays
'凤姐', '很美' => '芙蓉姐姐', 'verymei' => '王涛', '心中滴女神呀' => '杨澜', 100 => '孙俪', '娘娘', ]; //追加方式与索引数组是一样的 $drink['ynj'] = '伊能静'; //输出试试 echo ''; var_dump($drink); echo ''; //删除一个试试 unset($drink['verymei']); echo ''; var_dump($drink); echo ''; //将芙蓉姐姐 改成:心里美才是真的美 $drink['很美'] = '心里美才是真的美'; echo ''; var_dump($drink); echo ''; ?>
If you do the experiment just now, you will find out the operation methods and index operations Same way. It's just that the subscript is read a little differently.
Other ways of declaring associative arrays
$cocacola, ); echo ''; var_dump($drink); echo ''; ?>
Through the above example, we found that in associative arrays, array can also be used directly behind the variable. Connect the brackets. Inserting the string subscript inside the brackets also declares success.
The example of inserting variables later just turns the string into a variable, of course there is no problem.
Inserting an array into an array
Arrays can be inserted into integers, floating point, and strings. So, can arrays be inserted into arrays?
sure.
When learning arrays, we defined such a group of nouns.
1. One-dimensional array There are no other arrays in the array, only some variables or values.
2. A single-layer array or multiple arrays are inserted into the two-dimensional array array
3. An array (B) is inserted into the three-dimensional array into the array (A). A one-level array (C) is inserted into the B array, which we call a three-dimensional array
4. Anything with more than three dimensions is called a multi-dimensional array.
Note: Other arrays can be inserted into both index arrays and associative arrays to make the arrays multi-dimensional.
Let's declare a one-dimensional array with only one dimension. Both indexing and association are possible. Everyone is familiar with this operation.
250]; //一维的关联数组 $rela = [ 'beijing' => '北京', 'shanghai' => '上海', 'tj' => '天津', ]; echo ''; var_dump($rela); echo ''; echo ''; var_dump($data); echo ''; ?>
Everyone is familiar with one-dimensional arrays, and it is not difficult. Just declare the array. It's not difficult to make it two-dimensional, just insert one or more arrays into the array. If you learn two dimensions well, you will learn multidimensional ones well.
'办公室', //注意:插入第一个数组哟 'family' => array( '爸爸', '妈妈', 'yeye' => '爷爷', 'nn' => '奶奶', ), //注意:又插入了一个平级的数组 'jiaotong' => array( '自行车', '摩托车', '汽车', '飞机', ), ); echo ''; var_dump($person); echo ''; ?>
Let’s execute it and see the effect:
We found that the variable person is an array type. There are three elements (size=3).
The first element is office, which corresponds to a string "office",
The second element is family, which is an array , this array has 4 elements, the values are: dad, mom, grandpa, grandma.
The third element is jiaotong, which is also an array with 4 elements, and the values are: bicycle, motorcycle, car, and airplane.
Okay, the two-dimensional statement is good. As long as the format is written correctly and you make sure you insert an array into an array.
How to access: What about the two values of grandfather and father?
'办公室', //注意:插入第一个数组哟 'family' => [ '爸爸', '妈妈', 'yeye' => '爷爷', 'nn' => '奶奶', ], //注意:又插入了一个平级的数组 'jiaotong' => [ '自行车', '摩托车', '汽车', '飞机', ], ]; //访问“爸爸”这什值 echo $person['family'][0]; echo '
-----华丽丽的分割线------
'; //访问“爷爷”这什值 echo $person['family']['yeye']; echo '
-----华丽丽的分割线------
'; //访问“汽车”这什值 echo $person['jiaotong'][2]; ?>
Let’s look at the results as follows:
In the above example, you will find that accessing the two-dimensional array is just to follow the previous subscript reading method. Just click below to read.
Write the variable first, write the subscript family in square brackets, and then write the subscript of the element to be accessed.
We have talked about the concept of three-dimensional array, so let’s start the experiment directly to see the effect:
array( '上海', '湖北', '天津', '北京' => array( 'hd' => '海淀', '朝阳', '房山', 'cp' => '昌平', ), '广东' => array( '深圳', '广州', '佛山', 'dg' => '东莞', ), ), 'usa' => array( '华盛顿', '旧金山', '纽约' => array( '曼哈顿区', '皇后区', '布鲁克林区', ), ), ); echo ''; var_dump($area); echo ''; ?>
Let’s execute it and see the effect:
Explanation:
There are two arrays under the variable $area, one for china and one for usa.
Shanghai, Hubei, Tianjin, and Beijing and Guangdong are inserted into the china array. And Beijing and Guangdong are another array. There are different elements in the two arrays of Beijing and Guangdong.
Insert Washington, San Francisco and New York in the USA into the array. And below New York is another array, describing the several districts below New York.
So, a three-dimensional array is to insert an array (A) into the array, and insert an array into the A array.
Let’s take a look at how to read the value inside.
array( '上海', '湖北', '天津', '北京' => array( 'hd' => '海淀', '朝阳', '房山', 'cp' => '昌平', ), '广东' => array( '深圳', '广州', '佛山', 'dg' => '东莞', ), ), 'usa' => array( '华盛顿', '旧金山', '纽约' => array( '曼哈顿区', '皇后区', '布鲁克林区', ), ), ); //读取华盛顿 echo $area['usa']['0']; //读取:布鲁克林 echo $area['usa']['纽约'][2]; //读取:昌平 echo $area['china']['北京']['cp']; //修改cp下标的值改为:西城区 $area['china']['北京']['cp'] = '西城区'; //输出看看原来昌平的值是否发生了变化 echo $area['china']['北京']['cp']; ?>
Through the above we found that it is not difficult to learn arrays.
Difficulties in learning multi-dimensional arrays:
Pay attention to the format and tidy up the line breaks and indentations of each dimension. It’s not easy to make mistakes.
[Remember]
The separator between array elements is a comma. When inserting an array into an array, do not write a semicolon (;) at the end