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:
<?php $shu = array(1 , 1.5 , true ,'天王蓋地虎,小雞燉蘑菇'); echo '<pre>'; var_dump($shu); echo '</pre>'; ?>
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.
<?php $kele = array('只有不斷努力才能博得未來',10 => 'NoAlike', 'PHP中文網(wǎng)' , '去PHP中文網(wǎng)學PHP', 19 => '鳳姐和芙蓉我都愛' , '楊冪我最愛'); //打印顯示$kele echo '<pre>'; var_dump($kele); echo '</pre>'; ?>
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.
<?php $kele = array( '只有不斷努力才能博得未來', 10 => 'NoAlike', 'PHP中文網(wǎng)' , '去PHP中文網(wǎng)學PHP', 19 => '鳳姐和芙蓉我都愛' , '楊冪我最愛' ); //打印顯示$kele echo '<pre>'; var_dump($kele); echo '</pre>'; ?>
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 the adding and modifying the index array and delete.
<?php $minren = array( '楊冪', '王珞丹', '劉亦菲', '黃圣依' ); //如何向這$minren這個數(shù)組中增加元素呢 //猜猜范冰冰的下標是多少? $minren[] = '范冰冰'; $minren[100] = '范爺'; //它的下標又為幾呢? $minren[] = '李晨'; ?>
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:
<?php $minren = array( '楊冪', '王珞丹', '劉亦菲', '黃圣依', '范冰冰' ); //假設(shè)我不喜歡:黃圣依,如何將黃圣依給刪掉掉呢? //如果刪除掉后范冰冰的下標為多少呢? //如果在后面再追加一個元素,會填掉:“黃圣依”留下來的空嗎? unset($minren[3]); $minren[] = '金星'; echo '<pre>'; var_dump($minren); echo '</pre>'; ?>
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.
<?php $minren = array( '楊冪', '王珞丹', '劉亦菲', '黃圣依', '范冰冰' ); $minren[5] = '范爺'; $minren[2] = '亦菲,不要嫁給韓國人好嗎?'; echo '<pre>'; var_dump($minren); echo '</pre>'; ?>
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.
<?php //直接寫一個變量后面加上中括號,聲明變量 $qi[] = '可口可樂'; $qi[10] ='百事可樂'; echo '<pre>'; var_dump($qi); echo '</pre>'; ?>
2. It’s too troublesome to write using array() every time. You don’t need to write array, it’s simpler.
<?php $minren = [ '楊冪', '王珞丹', 100 => '劉亦菲', '黃圣依', '范冰冰' ]; echo '<pre>'; var_dump($minren); echo '</pre>'; ?>
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.
<?php //聲明一下關(guān)聯(lián)數(shù)組 $rela = array( '帥' => '陳奕迅', '很帥' => '黃曉明', '灰常灰常帥' => '寧澤濤', '有男人味的大叔' => '吳秀波', ); //再來玩玩簡潔聲明 $drink = [ '美' => '鳳姐', '很美' => '芙蓉姐姐', 'verymei' => '楊冪', '心中滴女神呀' => '華妃', 100 => '孫儷', '娘娘', ]; // 輸出 $rela echo '<pre>'; var_dump($rela); echo '</pre>'; // 輸出$drink echo '<pre>'; var_dump($drink); echo '</pre>'; ?>
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
<?php $drink = [ '美' => '鳳姐', '很美' => '芙蓉姐姐', 'verymei' => '王濤', '心中滴女神呀' => '楊瀾', 100 => '孫儷', '娘娘', ]; //追加方式與索引數(shù)組是一樣的 $drink['ynj'] = '伊能靜'; //輸出試試 echo '<pre>'; var_dump($drink); echo '</pre>'; //刪除一個試試 unset($drink['verymei']); echo '<pre>'; var_dump($drink); echo '</pre>'; //將芙蓉姐姐 改成:心里美才是真的美 $drink['很美'] = '心里美才是真的美'; echo '<pre>'; var_dump($drink); echo '</pre>'; ?>
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
<?php $drink['nf'] = '農(nóng)夫山泉'; $cocacola = '可口可樂'; //當然可以是變量喲 $drink['kl'] = $cocacola; $data = array( 'kl' => $cocacola, ); echo '<pre>'; var_dump($drink); echo '</pre>'; ?>
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.
<?php //一維的索引數(shù)組 $data = [1 , 2 , 3 , 4 , 10 => 250]; //一維的關(guān)聯(lián)數(shù)組 $rela = [ 'beijing' => '北京', 'shanghai' => '上海', 'tj' => '天津', ]; echo '<pre>'; var_dump($rela); echo '</pre>'; echo '<pre>'; var_dump($data); echo '</pre>'; ?>
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.
<?php $person = array( 'office' => '辦公室', //注意:插入第一個數(shù)組喲 'family' => array( '爸爸', '媽媽', 'yeye' => '爺爺', 'nn' => '奶奶', ), //注意:又插入了一個平級的數(shù)組 'jiaotong' => array( '自行車', '摩托車', '汽車', '飛機', ), ); echo '<pre>'; var_dump($person); echo '</pre>'; ?>
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?
<?php $person = [ 'office' => '辦公室', //注意:插入第一個數(shù)組喲 'family' => [ '爸爸', '媽媽', 'yeye' => '爺爺', 'nn' => '奶奶', ], //注意:又插入了一個平級的數(shù)組 'jiaotong' => [ '自行車', '摩托車', '汽車', '飛機', ], ]; //訪問“爸爸”這什值 echo $person['family'][0]; echo '<br />-----華麗麗的分割線------<br />'; //訪問“爺爺”這什值 echo $person['family']['yeye']; echo '<br />-----華麗麗的分割線------<br />'; //訪問“汽車”這什值 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:
<?php $area = array( 'china' => array( '上海', '湖北', '天津', '北京' => array( 'hd' => '海淀', '朝陽', '房山', 'cp' => '昌平', ), '廣東' => array( '深圳', '廣州', '佛山', 'dg' => '東莞', ), ), 'usa' => array( '華盛頓', '舊金山', '紐約' => array( '曼哈頓區(qū)', '皇后區(qū)', '布魯克林區(qū)', ), ), ); echo '<pre>'; var_dump($area); echo '</pre>'; ?>
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.
<?php $area = array( 'china' => array( '上海', '湖北', '天津', '北京' => array( 'hd' => '海淀', '朝陽', '房山', 'cp' => '昌平', ), '廣東' => array( '深圳', '廣州', '佛山', 'dg' => '東莞', ), ), 'usa' => array( '華盛頓', '舊金山', '紐約' => array( '曼哈頓區(qū)', '皇后區(qū)', '布魯克林區(qū)', ), ), ); //讀取華盛頓 echo $area['usa']['0']; //讀?。翰剪斂肆?echo $area['usa']['紐約'][2]; //讀?。翰?echo $area['china']['北京']['cp']; //修改cp下標的值改為:西城區(qū) $area['china']['北京']['cp'] = '西城區(qū)'; //輸出看看原來昌平的值是否發(fā)生了變化 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