Note the text in the remarks section ~
Copy codeThe 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 and Indexes
// The type can be int or string. [What is int? query the php manual]
// You can write it like this
// $ Array = array (0 => 'A', 1 => 'B ');
// You can also write
// Array automatically adds the index key name. The default value is that the int value starts from 0.
$ Array = array ('A', 'B ');
// Echo cannot be used for testing. Only print_r can be used to print arrays.
Print_r ($ array );
// The output result is Array ([0] => a [1] => B)
// It can be seen that if you do not set the key name [key], it will automatically add the key
// You can change the key at will.
$ 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.
// Here echo is used because the echo output can be directly used as long as it is not an array.
// The 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 ['a']. Note that strings are enclosed by quotation marks.
// The value [value] can be a variable or 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.
// Read the content.
Echo $ array [0] [1];
// The result is B.
// Of course, it can also contain more Arrays
$ Array = array (0 => array ('A', 'B'), array ('C', 'D ')), 1 => array ('E', 'F'), array ('G', 'H ')));
// It looks a little messy and 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 cycles.
Echo '<br/> ';
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 );
// Only one array is returned. The last one replaces 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 10 [age] => my age 110 ))
// How to use it?
?>
<Ul>
<? Php
Foreach ($ array as $ value ){
Echo '<li>'. $ value ['name']. '|'. $ value ['age']. '</li> ';
}
?>
</Ul>
<? Php
// Html code is added to the preceding result.
// If you want to perform operations on the array, such as filtering out a total of 10 arrays in the above $ array, You need to delete the one with the key 3.
Unset ($ array [3]);
Print_r ($ array );
// You will see that 3 has no
// The above is a simple usage. Next time I will talk about some advanced
?>