Recently, I am teaching a friend php, who has no foundation for other languages. The understanding and usage of array are fuzzy. So I wrote a tutorial. For more information, see
Note the text in the remarks section ~ The Code is as follows: <? Php // instantiate an array $ array = array (); // The array consists of the following two parts: // array (key name [key] => value [value]) // key names are generally used with indexes. // The type can be int or string. [What is int? can query php manual] // so you can write this // $ array = array (0 => 'A ', 1 => 'B'); // You can also write // array will automatically add the index key name, the default value is int starting from 0 $ array = array ('A', 'B'); // The test cannot use echo. Only print_r can be used to print arrays. Do not ask why, print_r ($ array); // The output result is Array ([0] => a [1] => B) // you can see that, if you do not set the key name [key], it will automatically add the key // you can also change the key $ array = array (3 => 'A ', 5 => 'B'); print_r ($ array); // end If Array ([3] => a [5] => B) // if you want to read the array content, you can do this echo $ Array [3]; // The result is a // here echo is used as long as it is not an array, can be directly output with echo // key can be a string $ array = array ('A' => 'A', 'bb '=>' B '); print_r ($ array); // The result is Array ([aa] => a [bb] => B) // You can also echo $ array ['aa']; note that all strings must be enclosed in quotation marks. // The value [value] can be a variable, which can be an array $ array = array (0 => array ('A', 'B '), 1 => array ('C', 'D'); print_r ($ array ); // The result is Array ([0] => Array ([0] => a [1] => B) [1] => Array ([0] => C [1] => d) // This is called a two-dimensional array. // The echo $ array [0] [1] can be used to read the content. // The result is B. You can also use // or, of course, it can contain more arrays $ array = array (0 => array ('A', 'B '), array ('C', 'D'), 1 => array ('E', 'F'), array ('G ', 'h'); // It looks a little messy. You need to understand it slowly. // return to the actual application to instantiate a rent $ array = array (); // simulate an SQL loop. Most SQL statements use a while loop. Here I will make a simple for 10 loop echo '<br/> '; for ($ I = 0; $ I <= 10; $ I ++) {$ array [] = array ('name' => 'my name '. $ I, 'age' => 'my age '. $ I); // $ array [] brackets Is to let him generate 10 arrays 0-10 // if it is $ array = array ('name' => 'my name '. i, 'age' => 'my age '. i); // there is only one array. The last one will replace the previous one} print_r ($ array ); // result Array ([0] => Array ([name] => my name 0 [age] => my age 10) [1] => Array ([name] => my name 1 [age] => my age 11) [2] => Array ([name] => my name 2 [age] => my age 12) [3] => Array ([name] => my name 3 [age] => my age 13) [4] => Array ([name] => my name 4 [age] => my age 14) [5] => Array ([name] => my name 5 [age] => my age 15) [6] => Array ([name] => my name 6 [age] => my age 16) [7] => Array ([name] => My name 7 [age] => my age 17) [8] => Array ([name] => my name 8 [age] => my age 18) [9] => Array ([name] => my name 9 [age] => my age 19) [10] => Array ([name] => my name is 10 [age] => my age is 110) // how can I use it?> <Ul> <? Phpforeach ($ array as $ value) {echo '<li> '. $ value ['name']. '| '. $ value ['age']. '</li>' ;}?> </Ul> <? Php // the above results are added with the html code. Let's put it apart. // if you want to operate the array, for example, filter the above $ array, there are a total of 10 arrays, now you need to delete the unset ($ array [3]); print_r ($ array ); // you will see that 3 does not have it. // The above is a simple usage. What are the advanced ones next time?>