Table of Contents
th".($i+1)."The individual's name is {$arr[$i]}
{$key}:{$val}
提交用户信息
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

Jul 25, 2016 am 08:51 AM

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


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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles