PHP array definitions and traversal, PHP array functions and multidimensional arrays

Source: Internet
Author: User
Tags array definition form post
PHP array definition and array traversal, PHP Array function usage and examples, PHP array value assignment, PHP multidimensional array of the loop output, etc., for everyone to learn reference.

1, PHP array definition and traversal 2, PHP array function

1. Array definition:

$arr =array;//index array, subscript is all digital $arr=array ("name" = "user1", "age" = "30");//associative array, subscript contains letters//subscript only two, or letters, If it's a number without double quotes, 1,3, "age" =>4,5,100=>6,7,400=>8,9); echo "
";  Print_r ($arr);  echo "
";?>

2, array subscript: If it is 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[100] [6[101] = 7[400] + 8[401] = 9

3, 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

Section ". ($i + 1). " The name of the individual is {$arr [$i]}"; } ? >

Cyclic plus judgment:

Section ". ($i + 1). " The name of the individual is {$arr [$i]}"; } else{echo "

Section ". ($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;} We recommend using foreach to iterate through an array

Multidimensional arrays: 1. A one-dimensional array $arr =array (0];2), a two-dimensional array $arr =array (1,2,array (4,5)), $arr [2][0];2. Two-D arrays $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. "
"; } } ? >//Recommended use of 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 query database:

"; Print_r ($row 1); echo "";?>

Hyper Global Array: Hyper 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_n AME] + localhost//server domain [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 get data submitted with get http://localhost/index.php?id=10&name=user1 two pages communication: 1. Table only son value the first: GET method the second type: Post mode 2.a Tag value can only be used in the Get mode a tag is recommended to use the Get method to submit a data form recommended to submit data using post MAGIC_QUOTES_GPC = On; indicates that when a GET request is turned on, the ' front plus \ ' in Get data is spoken.

Get instance: index.php

<title>Receive information</title> Junjun2
Junzai3
Junjun4
Junjun5

rev.php

<title>Receive information</title>

Welcome:

Name:

Age:

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

index.php

<title>Receive information</title>

Submit User Information

rev.php

Receive information

Welcome:

Name:

Age:

$_request get a or form get or post data. $_cookies the same page gets $_session the same variable on multiple pages gets to the $_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.

"; Print_r ($GLOBALS); echo" ";?>
  • Related Article

    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.