A concise tutorial on PHP array Arrays and usage examples (easy to understand) _php tutorial

Source: Internet
Author: User
Note the text in the comments section Oh ~
Copy CodeThe code is as follows: Instantiate an array
$array = Array ();
The array consists of two parts as follows
Array (key name [key]=> value [value])
Key names are commonly used with indexes
type can be int or string [int is what can be found in PHP manual]
So it can be written like this
$array = Array (0=> ' a ',1=> ' B ');
You can write that too.
Array automatically complements the index key name, which defaults to an int value starting from 0
$array = Array (' A ', ' B ');
Test cannot use Echo to print an array with Print_r don't ask why.
Print_r ($array);
The output is an Array ([0] = a [1] = b)
As you can see, if you do not set the key name [key] It will automatically replenish the key
You can also change the key as you feel
$array = Array (3=> ' a ',5=> ' B ');
Print_r ($array);
Result Array ([3] = a [5] = b)
If you want to read the contents of an 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, you can use echo output directly
Key can be a string
$array = Array (' AA ' = ' a ', ' bb ' = ' B ');
Print_r ($array);
The result is an array ([AA] + a [BB] = b)
So you can also echo $array [' AA ']; Note that the strings are enclosed in quotation marks
The value [value] can be a variable, which can be an array
$array = Array (0=>array (' A ', ' B '), 1=>array (' C ', ' d '));
Print_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 of the inside can be like this
echo $array [0][1];
The result is that B can also be used
Of course, it could be more arrays.
$array = Array (0=>array (' A ', ' B '), Array (' C ', ' d ')), 1=>array (Array (' E ', ' f '), Array (' G ', ' h ')));
It seems a little messy, you have to slowly understand
Regression to a real application instantiate a few leases
$array = Array ();
Simulating a SQL loop SQL mostly with a while loop, I'm doing a simple for 10 cycles here
Echo '
'; Echo '
';
for ($i =0; $i <=10; $i + +) {
$array [] = Array (' name ' = ' My Name '.) $i, ' age ' = ' I am 1 '. $i);
$array [] plus bracket is for him to generate 10 arrays of 0-10 respectively
If it is $array = Array (' name ' = ' My name '. I, ' age ' = ' I am 1 '. i);
Then the result is only one array. The last one will replace the previous one.
}
Print_r ($array);
The result array ([0] = = Array ([name] = + my name 0 [age] = My years) [1] = = Array ([name] = + my name 1 [age] = I am old one) [2] = = Array ([name] = + my Name 2 [age] = My Years) [3] = = Array ([name] = + my name 3 [age] = [13] = = Array ([name] = + my name 4 [age] = my old) [5] = = Array ([name] = + my name is 5 [age] = [6] = a Rray ([name] = + my name 6 [age] = I'm old) [7] = = Array ([name] = + my name is 7 [age] = my old) [8] = = Array ([ Name] = + my name 8 [age] = I'm old) [9] = = Array ([name] = + my name is 9 [age] = I am old) [ten] = = Array ([name] = > My name is [age] and I am 110)
How to use it?
?>


      foreach ($array as $value) {
      Echo '
    • '. $value [' name ']. ' | '. $value [' age ']. '
    • ';
      }
      ?>

The above results add HTML code, you extrapolate it
If you want to do an array of operations such as filtering the above $array in a total of 10 arrays, now you want to delete the key 3 of the bar
Unset ($array [3]);
Print_r ($array);
You're going to see that 3 of that is gone.
The above is a simple use, next time to say some advanced
?>

http://www.bkjia.com/PHPjc/743930.html www.bkjia.com true http://www.bkjia.com/PHPjc/743930.html techarticle Note the text in the comment section Oh ~ The Copy Code code is as follows:? PHP//Instantiate an array $array = array ();//The array consists of two parts as follows//array (key name [key]= value [value])//Key ...

  • 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.