PHP 6 array usage, php array usage

Source: Internet
Author: User
Tags array definition form post

PHP 6 array usage, php array usage

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.
<?php$arr=array("name"=>1,3,"age"=>4,5,100=>6,7,400=>8,9);echo "<pre>";print_r ($arr);echo "</pre>";?>



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 "<br>";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
<? Php $ arr [] = 1; $ arr [] = 2; $ arr [] = 3; $ arr [] = 4; $ arr [] = 5; $ arr [] = 6; for ($ I = 0; $ I <5; $ I ++) {echo "



Cyclically add 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 Loop
Foreach performs array traversal:
<? Php // the 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> "; print_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 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:

<?php    header("content-type:text/html;charset=utf-8");        $arr=array("a","b",array("c","d"),array("e"));        echo "<pre>";print_r($arr);echo "</pre>";echo "




3D 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>";print_r($arr);echo "</pre>";echo "



// 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:
<?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);    $row1 = mysql_fetch_assoc($result);        echo "<pre>";    print_r($row1);    echo "</pre>";    ?>





Ultra Global Array:
Ultra-Global Array
$ _ SERVER
$ _ GET
$ _ POST
$ _ REQUEST
$ _ FILES
$ _ COOKIES
$ _ SESSION
$ GLOBALS

$ _ SERVER View 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] => 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
2. a label to pass the value
Only the get method can be used.

We recommend that you use the get method to submit data for tag.
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

<Html> 



Rev. php
<Html> 



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

Index. php
<Html> 



Rev. php
<Html> 



$ _ 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.


<?php$username111="user1";function show(){$GLOBALS[username111]="USER2";}show();echo $username111;echo "<pre>";print_r($GLOBALS);echo "</pre>";?>


Reprinted please indicate the source: http://blog.csdn.net/junzaivip




Php array usage

First of all, your writing method is really different. Now PHP is more generic.

(1) PHP, a weak language, does not need to be initialized like a rigorous language. It is automatically initialized when used. Therefore, the array can be used after it is declared. In addition, sometimes it does not need to be declared, such as writing $ I = 1.25 directly. If an error is reported in a rigorous language, PHP can do this.
(2) The default index of the array starts from 0 and then increases from 1. Count ($ arr) is the number of array elements. If n is the number of elements in the current array, $ arr [n-1] is the largest element in the index. Adding $ arr [n] is equivalent to adding an element behind the array. In fact, you can write $ arr [] = $ ss, with the same effect.
(3) An algorithm concept problem means that a natural number can be divisible by a prime number smaller than it, but it is not a prime number.
(4) No program error ....

Usage of foreach traversal array in PHP

The first format traverses the given $ a array. In each loop, the value of the current unit is assigned to $ B and the pointer inside the array moves forward (so the next unit will be obtained in the next loop ).

Since PHP 5, you can easily add & before $ B to modify the array unit. This method will assign values by reference instead of copying a value. For example:
<? Php
$ Arr = array (1, 2, 3, 4 );
Foreach ($ arr as & $ value ){
$ Value = $ value * 2;
}
// $ Arr is now array (2, 4, 6, 8)
?>

Var_dump ($ arr );

The second method is to re-assign values to each unit in the original $ arr file. If you do not understand the unit, you can download a php Manual Online.

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.