Table of Contents
第".($i+1)."个人的名字是{$arr[$i]}
{$key}:{$val}
提交用户信息
Home php教程 php手册 PHP第六课 数组的用法

PHP第六课 数组的用法

Jun 06, 2016 pm 07:55 PM
php learn function Basic study array usage

学习概要: *了解基本的数组函数的使用 *懂得数组的遍历 *了解超全局数组的基本关系与使用 数组 1.数组定义和遍历 2.数组函数 数组定义: $arr=array(1,2,3);//索引数组,下标全是数字 $arr=array(name=user1,age=30);//关联数组,下标中包含字母 //下标只有两种

学习概要:

*了解基本的数组函数的使用

*懂得数组的遍历

*了解超全局数组的基本关系与使用


数组

1.数组定义和遍历
2.数组函数

数组定义:
$arr=array(1,2,3);//索引数组,下标全是数字
$arr=array("name"=>"user1","age"=>"30");//关联数组,下标中包含字母
//下标只有两种,要么是字母,要是是不带双引号的数字
<?php $arr=array("name"=>1,3,"age"=>4,5,100=>6,7,400=>8,9);
			echo "<pre class="brush:php;toolbar:false">";
			print_r ($arr);
			echo "
Copy after login
"; ?>


数组下标:
如果是字母
$arr=array("name"=>1,3,"age"=>4,5,100=>6,7,400=>8,9);
//下标打印:"name"   0 
[name] => 1
   [0] => 3
   [age] => 4
   [1] => 5
   [100] => 6
   [101] => 7
   [400] => 8
   [401] => 9


数组取值:
1.输出整个数组
print_r($arr)
Copy after login



2.输出数组中的某个值
$arr=array("name"=>1,3,"age"=>4,5,"100"=>6,7,"400"=>8,9);
			
				echo $arr['age'];
				echo "<br>";
				echo $arr[100];
Copy after login



3.数组赋值:
1.$arr['age']=30;
数组赋值也可以定义数组:
$arr[]=1;
$arr[]=2;

4.数组遍历:
1.for循环
<?php $arr[]=1;
				$arr[]=2;
				$arr[]=3;
				$arr[]=4;
				$arr[]=5;
				$arr[]=6;
				
				
			 	for($i=0;$i<5;$i++){
			 		
			 		echo "<h1>第".($i+1)."个人的名字是{$arr[$i]}";
			 	
			 	}
				?>
Copy after login



循环加判断:
	<?php $arr[]=1;
			$arr[]=2;
			$arr[]=3;
			$arr[]=4;
			$arr[]=5;
			$arr[]=6;
			
			
		 	for($i=0;$i<5;$i++){
		 		
		 		if($i%2==0){
		 				echo "<h1>第".($i+1)."个人的名字是{$arr[$i]}";
		 		}else{
		 			echo "<h1 id="第-i-个人的名字是-arr-i">第".($i+1)."个人的名字是{$arr[$i]}</h1>";
		 		}
		 	
		 	}
			?>
Copy after login


2.foreach循环
foreach 进行数组遍历:
<?php //键值对 name="user1" 就是数组下标和值,key和value
			
			$arr[&#39;name&#39;]="junzai";
			$arr[&#39;age&#39;]=20;
			$arr[&#39;sex&#39;]="man";
			$arr[]="abc";


			echo "<pre class="brush:php;toolbar:false">";	
			print_r ($arr);
			echo "
Copy after login
"; foreach($arr as $key=>$val){ $num++; if($num%2==1){ echo "

{$key}:{$val}

"; }else{ echo "

{$key}:{$val}

"; } } ?>


3.while....list ..each 循环遍历
while(list($key,$val)=each($arr)){
echo $key.$val;
}

//推荐使用foreach遍历数组

多维数组:
1.一维数组 $arr=array(1,2,3);
$arr[0];
2.二维数组 $arr=array(1,2,array(4,5));
$arr[2][0];
2.二维数组 $arr=array(1,2,array(3,array(4,5)));
$arr[2][1][0];


二维数组遍历:
<?php header("content-type:text/html;charset=utf-8");
			    
			    $arr=array("a","b",array("c","d"),array("e"));
			    
			    echo "<pre class="brush:php;toolbar:false">";
				
				print_r($arr);	
				
				echo "
Copy after login
"; echo "
"; foreach($arr as $val){ if(is_array($val)){ foreach($val as $val2){ echo $val2."
"; } } else{ echo $val."
"; } } ?>



三维数组取值:
<?php header("content-type:text/html;charset=utf-8");
			    
			    $arr=array("a","b",array("c","d"),array("e",array("f","z")));
			    
			    echo "<pre class="brush:php;toolbar:false">";
				
				print_r($arr);	
				
				echo "
Copy after login
"; 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."
"; } } ?>


//推荐使用一维数组和二维数组
一张数据表其实就是一个二维数组,里面的每一行记录就是一个一维数组

查询数据库:
<?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 class="brush:php;toolbar:false">";
			    print_r($row1);
			    echo "
Copy after login
"; ?>




超全局数组:
超全局数组
$_SERVER
$_GET
$_POST
$_REQUEST
$_FILES
$_COOKIES
$_SESSION
$GLOBALS

$_SERVER 查看服务器信息
<?php header("content-type:text/html;charset=utf-8");
				    
				    echo "<pre class="brush:php;toolbar:false">";
				    print_r($_SERVER);
				    echo "
Copy after login
"; ?>


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_ADDR] => 127.0.0.1//服务器ip
   [SERVER_PORT] => 80//端口号
   [REMOTE_ADDR] => 127.0.0.1  //客户端访问ip
   [DOCUMENT_ROOT] => E:/AppServ/www
   [SERVER_ADMIN] => goxuexi@126.com
   [SCRIPT_FILENAME] => E:/AppServ/www/index.php  //脚本文件名字的绝对路径
   [REMOTE_PORT] => 49881
   [GATEWAY_INTERFACE] => CGI/1.1
   [SERVER_PROTOCOL] => HTTP/1.1
   [REQUEST_METHOD] => GET
   [QUERY_STRING] =>                   //请求字符串
   [REQUEST_URI] => ///请求url地址
   [SCRIPT_NAME] => /index.php//脚本名称(相对网站根目录)
   [PHP_SELF] => /index.php
   [REQUEST_TIME] => 1407568551//访问时间
   [argv] => Array
       (
       )


   [argc] => 0
)

$_GET 获取用get提交过来的数据

http://localhost/index.php?id=10&name=user1

两个页面之间通讯:
1.表单传值
第一种:get方式
第二种:post方式

只能用get方式


表单推荐使用post方式提交数据


 magic_quotes_gpc = on;表示开启了get请求的时候,会讲get数据中的'前面加\


get实例:
index.php

				 
				<title>
				接收信息
				</title>
				
				
				<a href="rev.php?name=junjun2&age=26" target="'_blank">junjun2</a><br>
				<a href="rev.php?name=junjun3&age=23" target="'_blank">junzai3</a><br>
				<a href="rev.php?name=junjun4&age=25" target="'_blank">junjun4</a><br>
				<a href="rev.php?name=junjun5&age=27" target="'_blank">junjun5</a><br>
				
				
			
Copy after login



rev.php
					 
					<title>
					接收信息
					</title>
					
					
					<h1>欢迎:
					<?php echo $_GET[&#39;name&#39;];?>
					</h1>
					<hr>
					<h1>姓名:<?php echo $_GET[&#39;name&#39;]?>
</h1>
					<h1>年龄:<?php echo $_GET[&#39;age&#39;]?>
</h1>
					
					
					
Copy after login



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

index.php
				 
				<title>
				接收信息
				</title>
				
				
				<h1 id="提交用户信息">提交用户信息</h1>
				 
Copy after login
姓名:
年龄:



rev.php
					 
					<header content-type="text/html">
					<title>
					接收信息
					</title>
					
					
					<h1>欢迎:
					<?php echo $_POST[&#39;name&#39;];?>
					</h1>
					<hr>
					<h1>姓名:<?php echo $_POST[&#39;name&#39;]?>
</h1>
					<h1>年龄:<?php echo $_POST[&#39;age&#39;]?>
</h1>
					
					
				</header>
Copy after login



$_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改变全局变量的值.


<?php $username111="user1";
					
					function show(){
					$GLOBALS[username111]="USER2";
					}
					
					show();
					
					echo $username111;
					
					echo "<pre class="brush:php;toolbar:false">";
					print_r($GLOBALS);
					echo "
Copy after login
"; ?>



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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles