Skip to Content
PHP编程:第4版
book

PHP编程:第4版

by Kevin Tatroe, Peter MacIntyre
January 2021
Intermediate to advanced
505 pages
9h 45m
Chinese
Publishing House of Electronics Industry
Content preview from PHP编程:第4版
126
5
数组
正如在第
2
章讨论的那样,
PHP
支持标量的数据类型(由单一的值来表示)和复合数
据类型(其中可能含有多种数据类型和多个值)。本章将讨论复合数据类型之一
数组
array
)。数组是数据值的集合,按有序的“键
-
值”对应关系来组织。下面这个不太严
格的例子或许可以帮助你理解数组 :可以把它想象成盛放有一些鸡蛋的蛋盒,蛋盒的每
个隔间可以存放一个鸡蛋,同时这些隔间需要在同一个容器里面。并且正如这些蛋盒不
一定必须盛放鸡蛋(可以放入任何东西,例如,石子、雪球、四片叶的三叶草或者螺母
螺栓等),对于数组来说,不局限于一种数据类型。它可以包含字符串、整型数、布尔
值等类型,另外,数组还可以包含其他数组,将来会支持更多类型。
本章将讨论如何创建数组,增加、删除数组元素,以及如何遍历数组内容。由于数组非
常普遍、非常有用,所以
PHP
中有很多用于数组的内置函数。例如,如果你想群发电子
邮件到多个电子邮件地址,就可以把这些地址存放到一个数组中,然后通过循环遍历该
数组将消息发送到相应的地址。同样,如果有一个允许多重选择的表单,则用户选择的
多项内容将被返回给数组。
索引数组和关联数组
PHP
中有两种数组——索引数组和关联数组。
索引
indexed
)数组的键(
key
,也
称为键名)是整数并且从
0
开始,当需要通过位置识别元素时可使用索引数组。
关联
associative
)数组的键是字符串,像有两个列的数据表,第一列是用于访问键值的键
key
),第二列是键值(
value
)。
PHP
内部将所有的数组都存储为关联数组,所以关联数组和索引数组的唯一区别在于键 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

解密金融数据

解密金融数据

Justin Pauley
算法技术手册(原书第2 版)

算法技术手册(原书第2 版)

George T.Heineman, Gary Pollice, Stanley Selkow
Java持续交付

Java持续交付

Daniel Bryant, Abraham Marín-Pérez

Publisher Resources

ISBN: 9787121404634