Home php教程 php手册 PHP新手之PHP数组变量的使用

PHP新手之PHP数组变量的使用

Jun 13, 2016 am 11:14 AM
php one time learn use develop publish variable Multi-user I array newbie of

PHP经过长时间的发展,很多用户都很了解PHP了,这里我发表一下个人理解,和大家讨论讨论。花费大量时间浏览PHP的外部风景(学习了所有关于PHP的控制结构、操作符和变量的内容),对此,你可能已经厌烦了。你也许甚至考虑立即退出该教程,然后更愿意(大约你会这么认为)把时间花费在电视机前。

如果这样,那将会是一个大的错误。而当我说“大”的时候,我的意思是指巨大。 你看,如果你因为Ally McBeal的魅力而放弃该教程本章节内容的学习,那么你将会错失接触PHP最酷的变量类型之一的机会。它就是被称为“数组(array)”的一个小玩意,而且我毫不夸张的说,当我告诉你一旦你适应它的用法,你将对PHP脚本另眼相看。但是,不要把我的话当作……,把那些仍到一边,自己亲自试试看!截至目前,我们所讨论的变量只包含一个值,就像下面代码所示的一样

<ol class="dp-c"><li class="alt"><span><span><?php  </span><span class="vars">$i</span><span> = 5; ?> </span></span></span></li></ol>
Copy after login

然而,数组变量是完全不同的状况。数组是一种复杂的变量类型,其允许你在单一变量中存储多个值(当你需要存储和描述相关信息的时候,它可以很容易地做到)。我们可以把PHP数组变量当作可以容纳一个或多个值的“容器”变量。例如:

<ol class="dp-c"><li class="alt"><span><span><?php  </span><span class="comment">// define an array $pizzaToppings = array('onion', 'tomato', 'cheese'</span><span> </span></span></span></li></ol>
Copy after login

这里,$pizzaToppings是一个数组变量,其包含‘onion’、‘tomato’、‘cheese’、‘anchovies’、‘ham’和‘pepperoni’这些值(数组变量对于相关值进行分组尤其有用)。 Print_r()是一个特殊的函数,其允许你窥见PHP数组变量里面的值。和用于显示数组内容的目的比较而言,它对于程序调试(找出脚本失效的原因)更有用,但我将在这里使用它以使你可以理解表面之下所正在发生的。确保让你的服务器运行和浏览器打开,好吗?

数组里面不同的元素通过索引值来访问,且其第一个元素的索引值从0开始。因此,为了访问元素‘onion’,你将使用符号$pizzaToppings[0],而‘anchovies'将是$pizzaToppings[3](本质上是数组变量名称紧跟着被中括号所包含的索引值)。

PHP也允许你用用户自定义的“关键字”来代替索引,这是为了创建一种稍微不同类型的数组。其中,每个关键字都是唯一的,并且对应于数组中的一个单一值。

<ol class="dp-c">
<li class="alt"><span><span><?php  </span><span class="comment">// define an array $fruits = array('red' => 'apple', 'yellow' => 'banana', 'purple' =>  </span><span> </span></span></span></li>
<li>
<span class="string">'plum'</span><span>, </span><span class="string">'green'</span><span> => </span><span class="string">'grape'</span><span>); print_r(</span><span class="vars">$fruits</span><span>); ?> </span>
</li>
</ol>
Copy after login

在该实例中,$fruits是一个包含四个关键字值对的PHP数组变量。(=>符号用于指示关键字和其对应的值之间的关联关系)。为了访问 ‘banana’值,你要使用$fruits[‘yellow']符号,而‘grape’值通过符号$fruits[‘green']来访问。

该种类型的数组有时被称为“散列数组”或“关联数组”。如果你曾经使用过Perl语言,那么你将会看到它类似于Perl语言中的散列变量。

希望通过本文对PHP数组变量的介绍,能够给你带来一定的帮助。


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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 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 Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

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

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