A concise tutorial on array arrays and usages of PHP

Source: Internet
Author: User
Tags arrays php foreach

  recently taught a friend PHP that he has no basis for other languages. The understanding and usage of array is somewhat vague. So write a tutorial, need a friend can refer to the next

Note The comments section of the text introduction Oh ~     Code as follows: <?php//Instantiate an array $array = array (); The array consists of two parts as follows//array (key name [key]=> value [value])//Key name General with index//type can be int or string [int is what can look up PHP manual]//So can write//$array = Array (0=&gt ;' A ',1=> ' B '); You can do the same.//array automatically supplements the index key name, which defaults to the int value starting at 0 $array = Array (' A ', ' B '); Test does not use Echo to print arrays only with Print_r   don't ask why, do it right print_r ($array); The output is  array ([0] => a [1] => B)//Can be seen, if you do not set the key name [key] It will automatically add 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 contents of the array, you can do echo $array [3]; The result is a //here with Echo because as long as it is not an array, it can be directly with echo output//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 are enclosed in quotes/values [value] can be a variable, can be an array $array = Arrays (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 two-dimensional array//read the contents of the inside Can be like this echo$array [0][1]; The result is that B can also be used  //Of course it can also include more array $array = Array (0=>array (' A ', ' B '), Array (' C ', ' d ')), 1=>array (' e ', '), " F '), Array (' G ', ' h ')); It looks a little messy, and you have to understand it. Returns to the actual application instantiate a number of rent $array = Array (); Simulating a SQL loop SQL is mostly done with the while loop, where I do a simple for 10 cycles   echo ' <br/> '; Echo ' <br/> '; For ($i =0 $i <=10; $i + +) {  $array [] = Array (' name ' => ' My Name '. $i, ' age ' => ' my 1 '. $i);   //$array []   Plus bracket is going to let him generate 10 arrays respectively 0-10  //if is $array = Array (' name ' => ' My name '. I, ' age ' => ' my aged 1 '. i);   //So result only There is an array. The last one will replace the previous one} print_r ($array); Result Array ([0] => Array ([name] => my name 0 [age] => my Ages) [1] => Array ([name] => my name 1 [age] => my ages one) [2] => Array ([name] => my Name 2 [age] => my old one) [3] => Array ([name] => my name 3 [age] => my ages 13) [4] => Array ([name] => my name 4 [age] => my old) [5] => Array ([name] => my name 5 [age] => my age) [6] => A Rray ([name] => my name 6 [age] => me) [7] => ARRAy ([name] => my name 7 [age] => my old) [8] => Array ([name] => my name 8 [age] => my age) [9] => array ([NA Me] => My name 9 [age] => my old) [a] => Array ([name] => my name [age] => my aged 110))//How to use it??>   ;ul>   <?php foreach ($array as $value) {  echo ' <li> '. $value [' name ']. '  |   '. $value [' Age '].' </li> '; }  ?> </ul>   <?php//above results added HTML code, you extrapolate it//if you want to operate the array, such as filtering the above $array a total of 10 arrays, now you want to delete the key 3 That one unset ($array [3]);  Print_r ($array); You'll see the  3. The above is a simple usage, next time say some advanced?>  

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.