[转]PHP数组
我们学习的时间还不长,但有的会员已经迫不及待的想要实现很多网站功能,呵呵,有这样的要求和愿望很不错,这其实就是我们进步的动力。但是,作为一门编程语言,我们毕竟还是要掌握一些基本的规则,比如数据类型,语法等。好在PHP并不难,这些东西也不多,再学几课我们就可以开始利用它来实现一个一个网站功能了,到时候相信你会越来越有精神了。好,开始今天的课程。
今天我们学习数组,数组是一种数据类型,它的使用频率相当高,学会处理数组会让你做网站时得心应手。举个例子:你的网站数据库里存有大里的文章,现在你想在一个页面上显示20条娱乐新闻的标题,这20条标题从数据库里取出来后你想用20个变量表示吗?我想你不会这么笨吧,那么用什么变量能完全接收这20条数据呢?这就要用到数组变量。
什么是数组呢?数组实际上是一个数据集合,相当于是一个数据容器,很多数据存放在里面,我们可以按一定方法存进去或取出来,还可以对它里面的数据进行排序等各种操作,还可以检查里面有没有我们想要的数据等等。
数组的定义:
可以用 array() 语言结构来新建一个 array(数组)。它接受一定数量用逗号分隔的 key => value 参数对。例如8-1:
[复制到剪贴板]PHP代码:
$arr = array(1 => "新浪", 2 =>"网易", 3 => "腾讯", "雅虎");
?>
数组里面的数据实际上是按一定顺序排列的,每个数据都有一个key对应,这个key(键值)由自己决定,如果你没有给出key,系统会按序列分配一个键值(key)。这里的 "雅虎"我们没有给出键值,但系统会分配给它一个键值4。
既然系统能自动分配键值,可以不可以不写键值呢?当然可以,比如你可以这样写8-2:
[复制到剪贴板]PHP代码:
$arr = array( "新浪", "网易", "腾讯", "雅虎");
?>
这里要注意:系统分配键值(key)是从0开始的,你知道"新浪"的键值是什么吗?
如何访问数组数据:
上面那么多数据我们都用变量$arr表示了,要从中取出我们想要的数据应该怎么做呢?例如取出8-1例中的数据这么做8-3:
[复制到剪贴板]PHP代码:
$arr = array(1 => "新浪", 2 =>"网易", 3 => "腾讯", "雅虎");
echo $arr[1]; //这个会输出“新浪”
echo $arr[2]; //这个会输出“网易”
echo $arr[3]; //这个会输出“腾讯”
echo $arr[4]; //这个会输出“雅虎”
?>
就是用变量名加上中括号内不同的key访问不同的数据。中括号内的key我们也叫它下标。要得到8-2中的“新浪”应该怎么做?对了用$arr[0]。
用字符串作键名:
上面我们讲到的key(键值,键名)都是整数,PHP中规定,作为键名的只有两种:整数(integer)和字符串(string),那么用字符串作键名应该怎么做又如何访问其值呢?例如8-4:
[复制到剪贴板]PHP代码:
$arr = array("a" => "新浪", "b"=>"网易", "c" => "腾讯", "雅虎");
echo $arr['a']; //这个会输出“新浪”
echo $arr['b']; //这个会输出“网易”
echo $arr['c']; //这个会输出“腾讯”
echo $arr[0]; //这个会输出“雅虎”
?>
前面我们讲过,定义字符串要用引号,所以访问数组数据时中括号内的键名一定要用引号。
用方括号的语法新建/修改:
如果我们要添加一个数据或修改一个数据要怎么做呢?你可以通过明示地设定值来改变一个现有的数组。这是通过在方括号内指定键名来给数组赋值实现的。也可以省略键名,在这种情况下给变量名加上一对空的方括号(“[]”)。如8-5:
[复制到剪贴板]PHP代码:
$arr = array("a" => "新浪", "b"=>"网易", "c" => "腾讯", "雅虎");
$arr['a'] = "PHP中文社区";
$arr['e'] = "新浪";
$arr[] = "百度";
echo $arr['a']; //这个会输出“PHP中文社区”
echo $arr['b']; //这个会输出“网易”
echo $arr['c']; //这个会输出“腾讯”
echo $arr['e']; //这个会输出“新浪”
echo $arr[0]; //这个会输出“雅虎”
echo $arr[1]; //这个会输出“百度”
?>
引用地址:http://hi.baidu.com/cnwrol/blog/item/2cbed062599aa8dee6113a4b.html

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
