PHP array arrays and use examples concise tutorials (easy to understand) _php instance

Source: Internet
Author: User

Note The comments section of the text introduction Oh ~

Copy Code code as follows:
<?php
Instantiating an array
$array = Array ();
The array consists of two parts as follows
Array (key name [key]=> value [value])
Key names in general and index
type can be int or string [int is what can look up PHP manual]
So you can write this.
$array = Array (0=> ' a ',1=> ' B ');
You can write that, too.
Array automatically supplements the index key name, which defaults to the int value starting at 0
$array = Array (' A ', ' B ');
Test cannot use Echo to print arrays only with Print_r 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 change the key at your own discretion.
$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 so
Echo $array [3];
The result is a.
Echo is used here because as long as it's not an array, you can output it directly with Echo.
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 = 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))
It's 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 looks a little messy.
Regression to an actual application to instantiate a number of rents
$array = Array ();
Simulate a SQL loop SQL mostly with a while loop where I do a simple for 10 times loop
Echo ' <br/> '; Echo ' <br/> ';
For ($i =0 $i <=10; $i + +) {
$array [] = Array (' name ' => ' My Name '). $i, ' age ' => ' my aged 1 '. $i);
$array [] plus bracket is to let him generate 10 arrays, respectively 0-10
If it is $array = Array (' name ' => ' My name '. I, ' age ' => ' my aged 1 '. i);
So there's only one array of results. 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 age) [8] => Array ([ Name] => My name 8 [age] => my old [9] => Array ([name] => my name 9 [age] => my age) [] => Array ([name] = > My name [age] => 110)
How to use it?
?>

<ul>
<?php
foreach ($array as $value) {
Echo ' <li> '. $value [' name ']. '  | '. $value [' age ']. ' </li> ';
}
?>
</ul>

<?php
The above result adds the HTML code, you extrapolate
If you want to operate on an array such as filtering a total of 10 arrays in the $array above, now you want to delete the one that has a key of 3
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.