PHP lesson Sixth the use of arrays

Source: Internet
Author: User
Tags foreach array array definition arrays form post header sql variable

Study Summary:

* Understand the use of basic array functions

* Understand the traversal of arrays

* Understanding the basic relationships and use of hyper-global arrays


Array

1. Array definition and traversal
2. Array function

Array definition:
$arr =array (1,2,3);//index array, subscript all numbers
$arr =array ("name" => "user1", "Age" => "30");//associative array, subscript contains letters
There are only two kinds of subscript, or letters, if it's a number with no double quotes.
 
  1,3, "age" =>4,5,100=>6,7,400=>8,9);
			echo "
";
			Print_r ($arr);
			echo "
"; ? >



Array subscript:
If it's a letter,
$arr =array ("name" =>1,3, "age" =>4,5,100=>6,7,400=>8,9);
Subscript Printing: "Name" 0
[Name] => 1
[0] => 3
[Age] => 4
[1] => 5
[MB] => 6
[=>] 7
[=>] 8
[401] => 9


Array value:
1. Output the entire array
Print_r ($arr)



2. A value in the output array
$arr =array ("name" =>1,3, "age" =>4,5, "=>6,7", "=>8,9");
			
				echo $arr [' age '];
				echo "
"; echo $arr [100];



3. Array Assignment:
1. $arr [' Age ']=30;
Array assignment can also define an array:
$arr []=1;
$arr []=2;

4. Array Traversal:
1.for Loop
 
  Subclause ". ($i + 1). " The name of the individual is {$arr [$i]} ";
			 	
			 	}
				? >



Circular plus judgment:
	
 
  Subclause ". ($i + 1). " The name of the individual is {$arr [$i]} ";
		 		} else{
		 			echo "

Subclause ". ($i + 1). " The name of the individual is {$arr [$i]}

"; } } ? >


2.foreach Loop
foreach for array traversal:
 
  ";	
			Print_r ($arr);
			echo "
"; foreach ($arr as $key => $val) {$num + +; if ($num%2==1) {echo

{$key}:{$val}

"; } else{echo "

{$key}:{$val}

"; } } ? >


3.while....list.. Each loop traversal
while (list ($key, $val) =each ($arr)) {
echo $key. $val;
}

It is recommended to traverse an array using foreach

Multidimensional Arrays:
1. One-dimensional array $arr =array (1,2,3);
$arr [0];
2. Two-D array $arr =array (1,2,array (4,5));
$arr [2][0];
2. Two-D array $arr =array (1,2,array (3,array (4,5));
$arr [2][1][0];


Two-dimensional array traversal:
 
  ";
				
				Print_r ($arr);	
				
				echo "
"; echo " "; foreach ($arr as $val) {if (Is_array ($val)) {foreach ($val as $val 2) {echo $val 2. "
"; } } else{echo $val. "
"; } } ? >



Three-dimensional array values:
 
  ";
				
				Print_r ($arr);	
				
				echo "
"; echo " "; foreach ($arr as $val) {if (Is_array ($val)) {foreach ($val as $val 2) {if (Is_array ($val 2)) {foreach ($val 2 as $val 3) {echo $val 3. "
"; } } else {echo $val 2.
"; } } } else{echo $val. "
"; } } ? >


It is recommended to use one-dimensional arrays and two-dimensional arrays
A data table is actually a two-dimensional array in which each row is a one-dimensional array.

Query database:
 
  ";
			    Print_r ($row 1);
			    echo "
";?>




Super Global Array:
Super Global Array
$_server
$_get
$_post
$_request
$_files
$_cookies
$_session
$GLOBALS

$_server View server information
 
  ";
				    Print_r ($_server);
				    echo "
";?>


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] => localhost//Server domain name
[SERVER_ADDR] => 127.0.0.1//server IP
[Server_port] => 80//port number
[REMOTE_ADDR] => 127.0.0.1//Client Access IP
[Document_root] => e:/appserv/www
[Server_admin] => goxuexi@126.com
[Script_filename] => e:/appserv/www/index.php//script file name absolute path
[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.php//script name (relative to site root)
[Php_self] =>/index.php
[Request_time] => 1407568551//access time
[argv] => Array
(
)


[ARGC] => 0
)

$_get Gets the data submitted with get.

Http://localhost/index.php?id=10&name=user1

Two communication between pages:
1. Table conveys value
The first kind: Get Way
The second type: Post mode
2.a Label Pass Value
Can only be used in get mode

A label recommends using GET method to submit data
The form recommends submitting data using post


MAGIC_QUOTES_GPC = on; When a GET request is opened, the ' front plus \


Get instance:
index.php

				 
				
				Receive information
				
				
				
				Junjun2
Junzai3
Junjun4
Junjun5



rev.php
					 
					
					Receive information
					
					
					
					

Welcome:

Name:

Age:




Post instance
$_post: Get the data from the form POST

index.php
				 
				
				Receive information
				
				
				
				

Submit User Information

Name:
Age:



rev.php
					 
					
 
  
					
  
   
					Receive information
					
					
					
					
  
   

Welcome:

Name:

Age:




$_request
Gets the data for a or form get or post.

$_cookies
The same page gets on multiple pages

$_session
The same variable is fetched from multiple pages
$_files
Gets the files 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 $username values are changed by $globals[username]= "User2".

Instance: Use $globals to change the value of a global variable.


 
  ";
					Print_r ($GLOBALS);
					echo "
";?>

Reprint please indicate the source: HTTP://BLOG.CSDN.NET/JUNZAIVIP





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.