Home php教程 php手册 php核心技术-数组的使用

php核心技术-数组的使用

Jun 21, 2016 am 08:50 AM
foreach javascript nbsp php substr

 

数组:是一种数据类型。集合型。

一组具有相同类型的数据的集合。

一些 键值对的 有序集合。Key/value 键值对。

数组是由  元素 组成:

元素是一组键值对。其中 键 元素下标 值 元素 值。

遍历:从头到尾 依次去处理所有的数组元素。

定位数组元素的方式:

由于一个数组内 会存在很多元素,通常当我们定位数组的元素时,采用的形式都是数组的元素的 下标的方式。

但有时,尤其是在遍历(集中,循环处理数组内的每个元素时)时,我们采用的元素定位方式为 数组指针。 同一时刻,这个指针只能指向一个数组元素。

如何获得一个数组?如何定义一个数组?

        Array();

    arrayname[]=值;

通常情况下,我们在定义数组的元素时,都需要为元素指定下标,与值。

其中数组元素的下标,要求为整型 或者是字符串类型。

数组元素的值可以是任意数据类型。

 

下标:

通常都是整型和字符串型。但是如果意外的为数组元素设置了一个其他类型下标,php会尝试将其他类型的下标转换成合理的形式。但是不可以使用 数组和对象 作为下标。

 

如果在定义数组元素时,没有制定下标的话,意味着 我们需要使用整型下标。下标的大小是根据当前的数组结构而定的。会根据当前数组内,最大的整型下标的值来确定新的下标,方式是 +1的形式。如果当前数组内没有一个整型值,会从0 开始。

如果我们需要定义一个下标从1开始的数组,应该如何定义?

 

如果下标的定义 是一个可以被直接转化成 整型的字符串,那么这个字符串会被转化成整型:

 

与之相对,如果不能直接被转化成整型,则继续使用字符串类型。

 

可以使用 空字符串作为下标:可以使用 负数作为下标,但是不会使用这个负数作为生成下标的依据(不会在负数的基础上+1);

 

Tip:以上的测试 同样适用于与 [] 语法,也就是不同的数据类型,可以写到中括号内。

中括号内[],应该使用数组元素的下标来定位元素。

 

[表达式]内可以写表达式

 

 

也可以写常量:

但是 当我们使用字符串做下标时, 应该在字符串周围增加引号,否则则个标识符会首先被解析成常量,如果没有相应的常量,才会被认作字符串。会降低效率。甚至在存在相应常量时,业务逻辑会出现问题:

 

Tip:出现以上问题的原因 不是由于[]的解析造成的,而是php在解析常量时 都这么处理。

 

 

Php中由于元素的值可以是任意的数据类型,因此也可以是数组类型。当元素的值是数组类型时,我们可以称作为多维数组。

但是,本质上是没有多维数组的。上面所说的多维数组,只是某个数组元素的值为数组而已。

同样 如果是多维数组的话,可以使用多个[]去访问到某个元素的值:

 

 

数组的分类:

按照数组元素的索引类型分成:

索引数组和关联数组。

索引数组:元素的下标 都是数值的数组, 典型的是 从0开始的一系列的数组元素。

关联数组:元素的下标与元素的值有关联性,元素下标是字符串类型,字符串可以具有描述信息的功能。

 

 

但是由于php数组的特殊性,php的数组元素的下标 既可以数值索引也可以是字符串关联,可以同时存在。

 

 

因为php的数组,在数据结构上 是一个链表(hash表)实现的,是键值对。

由于数组内会包含多个元素,我们通常情况下都是使用的某个元素的值,因此总会遇到遍历数组的情况。

 

数组的遍历:

数组的变量基本上都是依赖于数组的指针完成。

当我们刚刚建立一个数组的时候,数组内部的指针是指向数组的第一个元素的。如果每次我们能够获得 数组指针所指向的元素的信息的话。那么我们能够从头到尾移动数组指针的话,就能够完成数组的遍历。

我们最常用的遍历数组的方法 就是 foreach 语言结构:

 

Foreach(需要遍历的数组 as 保存当前数组指针所指向的元素的值变量) {

    循环体。可以在这里处理当前遍历到的数组元素

} 

 

Foreach(需要遍历的数组 as 保存当前数组指针所指向的元素的下标变量 =>  保存当前数组指针所指向的元素的值变量) {

      循环体。可以在这里处理当前遍历到的数组元素

 

注意:在上面的代码中,我们并没有显示的执行 移动数组指针的操作,但是foreach会在每一次获得元素信息后,将当前的指针向下移动一位。并且在遍历完成后,foreach会将数组指针移动到数组的开头。可以保证一个数组可以被无限次的foreach。

如果是多维数组的话,应该如何遍历?

 

可以在foreach中 再次嵌套foreach达到效果:

 

Foreach的传递方式:值传递:

 

但是 与 第一个,值传递相对的 还支持引用传递:

 

如果需要修改原数组的话,还可以通过直接操作原数组的形式:

 

字符串函数:

少量普通字符串的处理,通常使用字符串处理函数完成

处理的方式,按照字节去处理。

Gbk编码 一个汉字占2个字节

Utf8编码 一个汉字占3个字节。

Strlen(); 长度

Substr(字符串,起始位置,截取长度):截取字符串

 

按照字符去处理。

例如javascript的字符串就是按照字符处理。

Php默认不能使用字符的方式处理,需要使用php的多字节字符扩展来实现。

Php.ini中:

 

Mb_strlen();

Mb_substr();

 

正则表达式

批量字符串处理,正则表达式。

验证用户输入的数据是否是邮箱? 是否符合某一个字符串规则。

常见的使用正则的地方:

表单验证 - 用户数据的验证(来自浏览器的数据,get,post)。会使用 javascript的正则 和 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 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)

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

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.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles