Home >Backend Development >PHP Tutorial >PHP array array and usage examples concise tutorial_PHP tutorial
I am currently teaching PHP to a friend who has no foundation in other languages. The understanding and usage of array is a bit vague. So I wrote a tutorial, friends who need it can refer to it
Pay attention to the text introduction in the comments section~ The code is as follows:value[value]) //Key names are generally used for indexing //The type can be int or string [you can check the PHP manual for what int is] //So you can write like this //$array = array(0=>'a',1=>'b'); //You can also write like this //array will automatically add the index key name, the default is int value starting from 0 $array = array('a','b'); //Test. You cannot use echo. You can only use print_r to print the array. Don’t ask why, just do it. 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 key //You can also 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 contents of the array, you can do this echo $array[3]; //The result is a // Echo is used here because as long as it is not an array, echo output can be used directly. //key can be a string $array = array('aa'=>'a','bb'=>'b'); print_r($array); //The result is Array ( [aa] => a [bb] => b ) //So you can also echo $array['aa']; Note that strings must be enclosed in quotes //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 //Reading the content inside can be like this echo $array[0][1]; //The result is b, which can also be used //Of course, it can also contain more arrays $array = array(0=>array(array('a','b'),array('c','d')),1=>array(array('e','f') ,array('g','h'))); //It seems a bit confusing, you have to figure it out yourself slowly //Return to actual application and instantiate a data renter $array = array(); //Simulate a sql loop. Most sql uses while loop. Here I will make a simple for 10 times loop. echo '