Array usage of PHP Lesson 6

Source: Internet
Author: User
Learning Overview: * understanding the use of basic array functions * understanding the traversal of arrays * understanding the basic relationship of ultra-global arrays and using arrays 1. array definition and traversal 2. array Function Array definition: $ arrarray (, 3); index array, subscript is all numbers $ arrarray (nameuser1, age30); associated array, subscript contains only two letter subscript

Learning Overview: * understanding the use of basic array functions * understanding the traversal of arrays * understanding the basic relationship of ultra-global arrays and using arrays 1. array definition and traversal 2. array Function array definition: $ arr = array (1, 2, 3); // index array, subscript is all numbers $ arr = array (name = user1, age = 30 ); // associate the array. The subscript contains only two types of letters // subscript

Learning Overview:

* Understand how to use basic array Functions

* Understanding how to traverse Arrays

* Understand the basic relationships and usage of ultra-global arrays


Array

1. Define and traverse Arrays
2. array Functions

Array definition:
$ Arr = array (1, 2, 3); // index array, subscript is all numbers
$ Arr = array ("name" => "user1", "age" => "30"); // associate an array. The subscript contains letters.
// There are only two subscripts, either letters or numbers without double quotation marks.
 1,3,"age"=>4,5,100=>6,7,400=>8,9);echo "
";print_r ($arr);echo "
";?>



Array Subscript:
If it is a letter
$ Arr = array ("name" => 100, "age" => 400, => 6, 7, => );
// Subscript printing: "name" 0
[Name] => 1
[0] => 3
[Age] => 4
[1] => 5
[2, 100] => 6
[101] => 7
[400] => 8
[401] => 9


Array value:
1. output the entire array
print_r($arr)



2. Output a value in the array
$arr=array("name"=>1,3,"age"=>4,5,"100"=>6,7,"400"=>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
 



Cyclically add judgment:
 


2. foreach Loop
Foreach performs 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;
}

// We recommend that you use foreach to traverse the array.

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


Two-dimensional array traversal:
 ";print_r($arr);echo "
"; Echo" "; foreach ($ arr as $ val) {if (is_array ($ val) {foreach ($ val as $ val2) {echo $ val2 ."
";}} Else {echo $ val ."
";}}?>



3D array values:
 ";print_r($arr);echo "
"; Echo" "; foreach ($ arr as $ val) {if (is_array ($ val) {foreach ($ val as $ val2) {if (is_array ($ val2) {foreach ($ val2 as $ val3) {echo $ val3 ."
";}} Else {echo $ val2 ."
";}}} Else {echo $ val ."
";}}?>


// One-dimensional array and two-dimensional array are recommended.
A data table is actually a two-dimensional array, and each row of records in it is a one-dimensional array.

Query the database:
 ";    print_r($row1);    echo "
";?>




Ultra Global Array:
Ultra-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 Address
[SERVER_PORT] => 80 // port number
[REMOTE_ADDR] => 127.0.0.1 // client access ip Address
[DOCUMENT_ROOT] => E:/AppServ/www
[SERVER_ADMIN] => goxuexi@126.com
[SCRIPT_FILENAME] => E:/AppServ/www/index. php // absolute path of the script file name
[REMOTE_PORT] = & gt; 49881
[GATEWAY_INTERFACE] = & gt; CGI/1.1
[SERVER_PROTOCOL] => HTTP/1.1
[REQUEST_METHOD] => GET
[QUERY_STRING] => // request string
[REQUEST_URI] => // request url
[SCRIPT_NAME] =>/index. php // Script Name (relative to the website root directory)
[PHP_SELF] =>/index. php
[REQUEST_TIME] => 1407568551 // access time
[Argv] => Array
(
)


[Argc] => 0
)

$ _ GET get GET the data submitted with get

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

Communication between two pages:
1. form pass Value
First: get Method
Method 2: post

Only the get method can be used.


We recommend that you use post to submit data for a form.


Magic_quotes_gpc = on; indicates that when the get request is enabled, the 'prefix \


Get instance:
Index. php

 Receive informationJunjun2
Junzai3
Junjun4
Junjun5



Rev. php
 Receive informationWelcome:
 Name:
 Age:
 



Post instance
$ _ POST: Get the data from the form post

Index. php
 Receive informationSubmit user information



Rev. php
 
 
  Receive informationWelcome:
  Name:
  Age:
  
 



$ _ REQUEST
Obtain data from a, form get, or post.

$ _ COOKIES
Obtain the same page from multiple pages

$ _ SESSION
The same variable is obtained on multiple pages.
$ _ FILES
Obtain the file in the form and generate an array.

$ GLOBALS
$ GLOBALS [_ SERVER]
$ GLOBALS [_ GET]
$ GLOBALS [_ POST]
$ GLOBALS [_ FILES]
$ GLOBALS [_ REQUEST]
$ GLOBALS [_ COOKIES]
$ GLOBALS [username] // contains global variables on the page and changes the value of $ username through $ GLOBALS [username] = "user2.

Instance: use $ GLOBALS to change the value of the global variable.


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



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.