Home > Backend Development > PHP Tutorial > PHP array definition and traversal, PHP array functions and multi-dimensional arrays

PHP array definition and traversal, PHP array functions and multi-dimensional arrays

WBOY
Release: 2016-07-25 08:51:43
Original
1208 people have browsed it
The definition of PHP array and array traversal, the usage and examples of PHP array functions, PHP array value assignment, the loop output of PHP multi-dimensional array, etc. are provided for your study and reference.

1. PHP array definition and traversal 2. php array function

1. Array definition:

$arr=array(1,2,3);//Index array, all subscripts are numbers $arr=array("name"=>"user1","age"=>"30");//Associative array, the subscript contains letters //There are only two kinds of subscripts, either letters or numbers without double quotes 1,3,"age"=>4,5,100=>6,7,400=>8,9); echo "
";  
print_r ($arr);  
echo "
Copy after login
"; ?>

2. Array subscript: if it is 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 [100] => 6 [101] => 7 [400] => 8 [401] => 9

3. 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 arrays: $arr[]=1; $arr[]=2;

4.Array traversal: 1.for loop

The first ".($i+1)."The individual's name is {$arr[$i]}"; } ?>

Loop plus judgment:

The first ".($i+1)."The individual's name is {$arr[$i]}"; }else{ echo "

th".($i+1)."The individual's name is {$arr[$i]}

"; } } ?>

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; } //It is recommended to use foreach to traverse the array

Multidimensional Arrays: 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(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 $val2){ echo $val2."
"; } } else{ echo $val."
"; } } ?>

Three-dimensional array value:

"; 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."
"; } } ?> //It is recommended to use one-dimensional array and two-dimensional array

A data table is actually a two-dimensional array, and each row of records in it is a one-dimensional array. Query database:

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

Super global array: superglobal 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 //The absolute path of the script file name [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 website root directory) [PHP_SELF] => /index.php [REQUEST_TIME] => 1407568551//Access time [argv] => Array ( ) [argc] => 0 ) $_GET gets the data submitted using get http://localhost/index.php?id=10&name=user1 Communication between two pages: 1. Form value passing The first: get method The second way: post method 2.a tag passing value You can only use the get method The a tag recommends using the get method to submit data. It is recommended to use post method to submit data in forms. magic_quotes_gpc = on; means that when the get request is enabled, the ' in the get data will be preceded by

get instance: index.php

接收信息 junjun2
junzai3
junjun4
junjun5

rev.php

接收信息

欢迎:


姓名:

年龄:

post实例 $_POST:获取表单post过来的数据

index.php

接收信息

提交用户信息

姓名:
年龄:

rev.php

接收信息

欢迎:


姓名:

年龄:

$_REQUEST 获取a或者表单get或post过来的数据. $_COOKIES 同一个页面在多个页面获取 $_SESSION 同一个变量在多个页面获取到 $_FILES 获取表单中的文件,并生成一个数组. $GLOBALS $GLOBALS[_SERVER] $GLOBALS[_GET] $GLOBALS[_POST] $GLOBALS[_FILES] $GLOBALS[_REQUEST] $GLOBALS[_COOKIES] $GLOBALS[username]//包含页面内的全局变量,并且通过$GLOBALS[username]="user2"改变$username的值.

例子,使用$GLOBALS改变全局变量的值.

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


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template