Home >Backend Development >PHP Tutorial >PHP array array and usage examples concise tutorial_PHP tutorial

PHP array array and usage examples concise tutorial_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:35:23834browse

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 '
'; echo '
'; for($i=0;$i<=10;$i++){ $array[] = array('name'=>'My name'.$i,'age'=>'My age 1'.$i); // $array[] adds square brackets to allow it to generate 10 arrays, 0 - 10 respectively. //If it is $array = array('name'=>'My name'.i,'age'=>'My age 1'.i); //Then the result 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 is 16) [7] => Array ( [name] => My name is 7 [age] => My age is 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 name10 [age] => My age110 ) ) //How to use it? ?> ​
    '.$value['name'].' | '.$value['age'].''; } ?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/744062.htmlTechArticleI am teaching php to a friend recently, and he 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 in the comment part...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn