A concise tutorial _php tutorial on PHP array arrays and usage examples

Source: Internet
Author: User
Recently teaching a friend PHP, he has no other language basis. The understanding and usage of array is somewhat blurred. So write a tutorial, need a friend can refer to the next

Note the text in the comments section, the code is as follows: value [value])//Key name commonly used with index//type can be int or string [int is what can be checked PHP manual]//so can write//$array = Array (0=> ' a ',1=> ' B ');//You can write The array will automatically replenish the index key name, which defaults to an int value starting with 0 $array = Array (' A ', ' B ');//test cannot use echo can only print an array with Print_r don't ask why, do it right print_r ($array);//Output is Array ([0] = a [1] = b)//You can see that if you do not set the key name [key] It will automatically replenish key//you can also change Key$array = Array (3=> ' a ',5=> ' B ') to;p Rint_r ( $array);//result Array ([3] = a [5] = b)//If you want to read the contents of the array, you can do so echo $array [3];//The result is a//here with Echo because as long as it is not an array, you can directly use the Echo Output/ /key can be a string $array = Array (' AA ' = = ' a ', ' bb ' = ' B ');p Rint_r ($array);//The result is an array ([AA] + a [BB] + B)//So you can also echo $array [' AA ']; Note that strings are enclosed in quotation marks//values [value] can be a variable, which can be an array of $array = Arrays (0=>array (' A ', ' B '), 1=>array (' C ', ' d '));p Rint_r ($array);// The result is an array ([0] = = Array ([0] = a [1] = = b) [1] = = Array ([0] = c [1] + D))//This is called a two-dimensional array//read the contents inside can be this The kind of echo $array [0][1];//result is B can also be used//or can also contain more arrays $array = Array (0=>array (' A ', ' B '), Array (' C ', ' d '), 1=>array (Array (' E ', ' f '), Array (' G ', ' h ')); /looks a little messy, toI slowly understand//return to the actual application of the instantiation of a number of rent $array = Array ();//Simulate a SQL loop SQL mostly with a while loop, I do a simple for 10 times loop Echo '
'; Echo '
' For ($i =0; $i <=10; $i + +) {$array [] = Array (' name ' = ' My Name '. $i, ' age ' = ' my 1 '. $i);//$array [] plus brackets are for him to generate 10 arrays 0-10//If it is $array = Array (' name ' = ' My Name '). I, ' age ' = ' I am 1 '. Then the result is only one array. The last one will take the place of the previous}print_r ($array);//result Array ([0] = = Array ([name] = + my name 0 [age] = My years) [1] = = Array ([name] = = My name is 1 [age] = I am old) [2] = = Array ([name] = + my Name 2 [age] = my old) [3] = = Array ([name] = = I 's name 3 [age] = I'm older) [4] = = Array ([name] = + my name 4 [age] = my old) [5] = = Array ([name] = + my name 5 [ages ] = my age [6] = = Array ([name] = + my name 6 [age] = my ages) [7] = = Array ([name] = + my name 7 [age] My age) [8] = = Array ([name] = + my name 8 [age] = my ages) [9] = = Array ([name] = + my name 9 [age] = my old 19) [Ten] = = Array ([name] = [age] = my 110)//How do I use it???>
      '. $value [' name ']. ' | '. $value [' age ']. ';} ?>

http://www.bkjia.com/PHPjc/744062.html www.bkjia.com true http://www.bkjia.com/PHPjc/744062.html techarticle recently teaching a friend PHP, he has no other language basis. The understanding and usage of array is somewhat blurred. So write a tutorial, need a friend can refer to the note section of the text ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.