Php array and simple tutorial on using instances. 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. if you need a friend, you can refer to the text in the notes section. 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: 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); // result 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 // this can be used to read the content in echo $ array [0] [1]; // 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'
'; Echo'
'; For ($ I = 0; $ I <= 10; $ I ++) {$ array [] = array ('name' => 'My name '. $ I, 'age' => 'My age '. $ I ); // $ array [] brackets are used to generate 10 arrays 0-10, respectively. // 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?>
'. $ Value ['name'].' | '. $ value ['age'].'';}?>
Bytes. The understanding and usage of array are fuzzy. So I wrote a tutorial. if you need it, please refer to the text in the notes section...