search
Homephp教程php手册一步一步学习PHP(8) php 数组

一步一步学习PHP(8) php 数组

Jun 13, 2016 pm 12:18 PM
phpstepforstudyIusBundlearrayunderstandof

1. PHP中的数组
与其把PHP中的数组理解为我们狭义上的“数组”,我觉得还不妨把这个数组一分为二,一者为我们常规上的数组,一者为我们的Dictionary。
2. 创建数组
如果数组不存在,那么向数组中存放值将会创建数组。

复制代码 代码如下:


$address[0]='北京';
$address[1]='上海';
$address[2]='南京';
$introduce['北京']='首都';
$introduce['上海']='国际大都市';
$introduce['南京']='不了解了';
?>


还有一种较为正统的方法,使用array()语言结构,这也是我倾向的一种方式:

复制代码 代码如下:


$address=array('北京','上海','南京');
$introduce=array('北京'=>'首都',
'上海'=>'国际大都市',
'南京'=>'不了解了'
);
?>


当然我们还可以用这样的方式创建一个空数组:

复制代码 代码如下:


$nullArray=array();
?>


3. 访问数组元素
访问数组元素其实和传统方式一样:

复制代码 代码如下:


$address=array('北京','上海','南京');
$introduce=array('北京'=>'首都',
'上海'=>'国际大都市',
'南京'=>'不了解了'
);
echo($address[1]);
echo($introduce['上海']);
?>


4. 遍历数组元素
遍历数组最常用的方式就是foreach,也比较通用。

复制代码 代码如下:


$address=array('北京','上海','南京');
$introduce=array('北京'=>'首都',
'上海'=>'国际大都市',
'南京'=>'不了解了'
);
foreach($address as $value)
{
echo($value.'
');
}
foreach($introduce as $key=>$value)
{
echo("$key => $value
");
}
?>


foreach遍历数组很容易,但是他有一个缺点,就是他并不是直接操作原数组,而是在遍历前先复制一份原数组的拷贝,这样就造成了时间和空间上的浪费。
那么有个简单的方法,就是for。

复制代码 代码如下:


$address=array('北京','上海','南京');
$introduce=array('北京'=>'首都',
'上海'=>'国际大都市',
'南京'=>'不了解了'
);
for($i=0;$i{
echo("$address[$i]
");
}
?>


这个虽然简单,但是也有缺点,就是只可以遍历索引数组,没有办法遍历字典。
于是,在PHP中提出了迭代器函数。
其中最常用的就是each()函数。看个简单的例子吧:

复制代码 代码如下:


$introduce=array('城市名'=>'介绍',
'北京'=>'首都',
'上海'=>'国际大都市',
'南京'=>'不了解了'
);
reset($introduce);
echo('

');
while(list($city,$intro)=each($introduce))
{
echo("");
}
echo('
$city $intro
');
?>

 
做个解释,each()函数用于遍历数组元素,类似于我们在常规意义上的迭代器。而且使用迭代函数的最大好处是并不会像foreach语言结构一样产生一个原数组的拷贝,在处理大数组时是很有用的。
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

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)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor