search
HomeBackend DevelopmentPHP TutorialPHP array definition and traversal, PHP array functions and multi-dimensional arrays

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 "
"; ?>

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 ""; ?>


Statement
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
Explain the concept of a PHP session in simple terms.Explain the concept of a PHP session in simple terms.Apr 26, 2025 am 12:09 AM

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

How do you loop through all the values stored in a PHP session?How do you loop through all the values stored in a PHP session?Apr 26, 2025 am 12:06 AM

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

Explain how to use sessions for user authentication.Explain how to use sessions for user authentication.Apr 26, 2025 am 12:04 AM

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.

Give an example of how to store a user's name in a PHP session.Give an example of how to store a user's name in a PHP session.Apr 26, 2025 am 12:03 AM

Tostoreauser'snameinaPHPsession,startthesessionwithsession_start(),thenassignthenameto$_SESSION['username'].1)Usesession_start()toinitializethesession.2)Assigntheuser'snameto$_SESSION['username'].Thisallowsyoutoaccessthenameacrossmultiplepages,enhanc

What are some common problems that can cause PHP sessions to fail?What are some common problems that can cause PHP sessions to fail?Apr 25, 2025 am 12:16 AM

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

How do you debug session-related issues in PHP?How do you debug session-related issues in PHP?Apr 25, 2025 am 12:12 AM

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

What happens if session_start() is called multiple times?What happens if session_start() is called multiple times?Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

How do you configure the session lifetime in PHP?How do you configure the session lifetime in PHP?Apr 25, 2025 am 12:05 AM

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor