Study Summary:
* Understand the use of basic array functions
* Understand the traversal of arrays
* Understand the basic relationship and use of hyper-global arrays
Array
1. Array definition and traversal
2. Array functions
Array definition:
$arr =array (a);Indexed arrays, subscripts are all numbers
$arr =array ("name" = "user1", "age" = "30");//associative array, subscript contains letters
There are two types of subscripts, either letters, or numbers without double quotes.
<?php$arr=array ("name" =>1,3, "age" =>4,5,100=>6,7,400=>8,9); echo "<pre>";p Rint_r ($arr); echo " </pre> ";? >
Array subscript:
If it's a letter
$arr =array ("name" =>1,3, "age" =>4,5,100=>6,7,400=>8,9);
Subscript Print: "Name" 0
[Name] = 1
[0] = 3
[Age] = 4
[1] = 5
[+] = 6
[101] = 7
[+] = 8
[401] = 9
Array values:
1. Output the entire array
Print_r ($arr)
2. Output a value in the array
$arr =array ("name" =>1,3, "age" =>4,5, "=>6,7", "the" =>8,9 "), Echo $arr [' Age '];echo ' <br> ', Echo $arr [100];
3. Assigning values to arrays:
1. $arr [' Age ']=30;
Array assignment can also define an array:
$arr []=1;
$arr []=2;
4. Array Traversal:
1.for Cycle
<?php$arr[]=1; $arr []=2; $arr []=3; $arr []=4; $arr []=5; $arr []=6; For ($i =0, $i <5; $i + +) { echo "
Cyclic plus judgment:
<?php$arr[]=1; $arr []=2; $arr []=3; $arr []=4; $arr []=5; $arr []=6; for ($i =0; $i <5; $i + +) { if ($i%2==0) {echo
2.foreach Cycle
foreach to iterate through the array:
The <?php//key value pair Name= "user1" is the array subscript and value, key and value$arr[' name ']= "Junzai"; $arr [' Age ']=20; $arr [' Sex ']= ' man '; $arr []= ' abc ' echo "<pre>";p Rint_r ($arr), echo "</pre>", foreach ($arr as $key + $val) {$num ++;if ($num%2==1) {echo "
3.while....list. Each loop traversal
while (list ($key, $val) =each ($arr)) {
echo $key. $val;
}
We recommend using foreach to iterate through an array
Multidimensional Arrays:
1. One-dimensional array $arr =array (All-round);
$arr [0];
2. Two-dimensional array $arr =array (1,2,array (4,5));
$arr [2][0];
2. Two-dimensional array $arr =array (1,2,array (3,array (4,5));
$arr [2][1][0];
Two-dimensional array traversal:
<?php Header ("Content-type:text/html;charset=utf-8"); $arr =array ("A", "B", Array ("C", "D"), Array ("E")); echo "<pre>";p Rint_r ($arr), echo "</pre>", echo "
Three-dimensional array values:
<?php Header ("Content-type:text/html;charset=utf-8"); $arr =array ("A", "B", Array ("C", "D"), Array ("E", Array ("F", "Z"))); echo "<pre>";p Rint_r ($arr), echo "</pre>", echo "
//recommend using one-dimensional arrays and two-dimensional arrays
A data sheet is actually a two-dimensional array in which each row of records is a one-dimensional array
Querying the database:
<?php Header ("Content-type:text/html;charset=utf-8"); mysql_connect ("localhost", "root", "1234"); mysql_select_db ("test"); mysql_query ("Set names UTF8"); $sql = "SELECT * from user"; $result = mysql_query ($sql); $row 1 = mysql_fetch_assoc ($result); echo "<pre>"; Print_r ($row 1); echo "</pre>"; ? >
Hyper-Global arrays:
Hyper-Global Array
$_server
$_get
$_post
$_request
$_files
$_cookies
$_session
$GLOBALS
$_server Viewing server information
<?php Header ("Content-type:text/html;charset=utf-8"); echo "<pre>"; Print_r ($_server); echo "</pre>"; ? >
apache/2.2.8 (WIN32) php/5.2.6 Server at localhost Port 80
[Server_software] = apache/2.2.8 (Win32) php/5.2.6
[server_name] = localhostServer domain name
[SERVER_ADDR] = 127.0.0.1Server IP
[Server_port] = 80Port number
[REMOTE_ADDR] = 127.0.0.1//Client Access IP
[Document_root] = e:/appserv/www
[Server_admin] = [email protected]
[Script_filename] = e:/appserv/www/index.php//absolute path to the name of the script file
[Remote_port] = 49881
[Gateway_interface] = cgi/1.1
[Server_protocol] = http/1.1
[Request_method] = = GET
[Query_string] =//Request string
[Request_uri] =/Request URL Address
[Script_name] =/index.phpScript name (relative to Web site root directory)
[Php_self] =/index.php
[Request_time] = 1407568551Access time
[argv] = = Array
(
)
[ARGC] = 0
)
$_get get data submitted with get
Http://localhost/index.php?id=10&name=user1
Two pages of communication between:
1. Table only son values
First type: Get mode
Second type: Post mode
2.a Label Transfer value
Can only be used in get mode
A tag recommends using GET method to submit data
The form recommends using post to submit data
MAGIC_QUOTES_GPC = on; indicates that when a GET request is turned on, the ' front plus \ ' In the Get data is spoken.
Get instance:
index.php
rev.php
Post instance
$_post: Get the data from the form POST
index.php
rev.php
$_request
Get the data for a or form get or post.
$_cookies
The same page gets on multiple pages
$_session
The same variable is obtained on multiple pages
$_files
Gets the file in the form and generates an array.
$GLOBALS
$GLOBALS [_server]
$GLOBALS [_get]
$GLOBALS [_post]
$GLOBALS [_files]
$GLOBALS [_request]
$GLOBALS [_cookies]
$GLOBALS [username]//contains global variables within the page and changes the value of $username by $globals[username]= "User2".
Example: Use $globals to change the value of a global variable.
<?php$username111= "User1"; function show () {$GLOBALS [username111]= "USER2";} Show (); Echo $username 111;echo "<pre>";p Rint_r ($GLOBALS); echo "</pre>";? >
Reprint Please specify source: HTTP://BLOG.CSDN.NET/JUNZAIVIP
PHP Sixth Lesson Array usage