Home > php教程 > php手册 > body text

PHP学习笔记-数组(1),php学习笔记数组

WBOY
Release: 2016-06-13 09:09:13
Original
959 people have browsed it

PHP学习笔记-数组(1),php学习笔记数组

1-1 数组定义

1.什么是数组?

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

语法如下:

<?<span>php
</span><span>//</span><span>设置某个变量为一个空数组</span>
    <span>$arr</span>=<span>array</span><span>();
</span>?>
Copy after login

1-2 索引数组初始化

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

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

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

<?<span>php
    </span><span>$num</span>=<span>array</span>(1,2,3<span>);
    </span><span>print_r</span>(<span>$num</span>[0<span>]);
</span>?>
Copy after login

输出结果:1


1-3 索引数组赋值

3种方法

<?<span>php
</span><span>$arr</span>[0]=1;<span>//</span><span>方法1</span>
<span>$arr</span>=<span>array</span>('0'=>1);<span>//</span><span>方法2</span>
<span>$arr</span>=<span>array</span>(1);<span>//</span><span>方法3</span>
?>
Copy after login

1-4 访问数组内容

<?<span>php
</span><span>//</span><span>从数组变量$arr中,读取键为0的值</span>
<span>$arr</span> = <span>array</span>(1,2,3<span>);
</span><span>$arr0</span>=<span>$arr</span>[0<span>];
</span><span>echo</span> <span>$arr0</span><span>;
</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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template