PHP学习笔记-数组(一)

WBOY
Release: 2016-06-13 12:17:01
Original
745 people have browsed it

PHP学习笔记-数组(1)

1-1 数组定义

1.什么是数组?

所谓数组,就是相同数据类型的元素按一定顺序排列的集合,就是把有限个类型相同的变量用一个名字命名,然后用编号区分他们的变量的集合,这个名字称为数组名,编号称为下标。组成数组的各个变量称为数组的分量,也称为数组的元素,有时也称为下标变量。

语法如下:

<span style="color: #000000;">php</span><span style="color: #008000;">//</span><span style="color: #008000;">设置某个变量为一个空数组</span>    <span style="color: #800080;">$arr</span>=<span style="color: #0000ff;">array</span><span style="color: #000000;">();</span>?>
Copy after login

 1-2 索引数组初始化

PHP有两种数组:索引数组、关联数组。

索引和关联两个词都是针对数组的键而言的。

索引数组是指数组的键是整数的数组,并且键的整数顺序是从0开始,依次类推。

<span style="color: #000000;">php    </span><span style="color: #800080;">$num</span>=<span style="color: #0000ff;">array</span>(1,2,3<span style="color: #000000;">);    </span><span style="color: #008080;">print_r</span>(<span style="color: #800080;">$num</span>[0<span style="color: #000000;">]);</span>?>
Copy after login

输出结果:1


1-3 索引数组赋值

3种方法

<span style="color: #000000;">php</span><span style="color: #800080;">$arr</span>[0]=1;<span style="color: #008000;">//</span><span style="color: #008000;">方法1</span><span style="color: #800080;">$arr</span>=<span style="color: #0000ff;">array</span>('0'=>1);<span style="color: #008000;">//</span><span style="color: #008000;">方法2</span><span style="color: #800080;">$arr</span>=<span style="color: #0000ff;">array</span>(1);<span style="color: #008000;">//</span><span style="color: #008000;">方法3</span>?>
Copy after login

1-4 访问数组内容

<span style="color: #000000;">php</span><span style="color: #008000;">//</span><span style="color: #008000;">从数组变量$arr中,读取键为0的值</span><span style="color: #800080;">$arr</span> = <span style="color: #0000ff;">array</span>(1,2,3<span style="color: #000000;">);</span><span style="color: #800080;">$arr0</span>=<span style="color: #800080;">$arr</span>[0<span style="color: #000000;">];</span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$arr0</span><span style="color: #000000;">;</span>?>
Copy after login

 


 

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!