PHP5中数组函数总结篇
我不知道PHP初学者是怎么学习函 数的,但是我认为学习他一定是有目的性,它是目的驱动的,以数组函数为例!这么多的数组函数我们该怎么学习?其实我们只需要清楚,我们需要对数组做哪些操 作,然后把这些操作归类,心中就有了大概的印象,然后随着你的实践,这些函数你就很清楚了,在这里提醒大家的是手册要常备!废话不多说,数组函数一般归为 以下几类(看到英文别怕哦):
Outputting arrays
Creating arrays
Testing for an array
Locating array elements
Traversing arrays
Determining array size and element uniqueness
Sorting arrays
Merging, slicing, splicing, and dissecting arrays
Outputting arrays
print_r()
不熟悉这个的我就不想多说了!最基本的函数,当然要输出数组也可是使用循环结构输出,不过有这么方便的输出函数干吗不用呢!
Creating arrays
array()
这个函数我也不想说了,这其实只能算语言结构,大家在新建数组的时候都用到过,没用过的我只能说,兄弟,我无语了!
list()
这个函数和上面提到的array()一样,只是个语言结构,它通过一步操作给一组变量赋值!
具体示例请查看手册!
range();
array range ( mixed low, mixed high[, number step] )
建立一个包含指定范围单元的数组
示例:$arr =range(0, 6);
相当于$arr =array(0, 1, 2, 3, 4, 5, 6);
一般数据按从低到高排列,如果low> high,则从高到低排列;
Testing for an array
is_array();
测试变量是否array类型,是则返回true,不是则返回false,很基本,很实用的函数!
Adding and removing array elements
array_push()
int array_push ( array &target_array,mixed var [, mixed ...] )
将一个或多个单元从末尾添加至数组!返回新数组的单元总数!
示例:
复制PHP内容到剪贴板
PHP代码:
<font face="新宋体">$states <span style="color: rgb(0,119,0)">=array(</span><span style="color: rgb(0,0,187)">‘Ohio’</span><span style="color: rgb(0,119,0)">, </span><span style="color: rgb(0,0,187)">‘New York’</span></font><font face="新宋体"><span style="color: rgb(0,119,0)">);<br>
</span><span style="color: rgb(0,0,187)">array_push</span><span style="color: rgb(0,119,0)">(</span><span style="color: rgb(0,0,187)">$states</span><span style="color: rgb(0,119,0)">, </span><span style="color: rgb(0,0,187)">‘California’</span><span style="color: rgb(0,119,0)">,</span><span style="color: rgb(0,0,187)">‘Texas’</span></font><font face="新宋体"><span style="color: rgb(0,119,0)">);<br>
</span><span style="color: rgb(255,128,0)">//output: array((‘Ohio’,‘New York’ , ‘California’,‘Texas’);</span><br>
<br>
</font>
array_pop();
弹出并返回数组的最后一个单元,并将数组长度减一。使用后会重置数组指针!
示例:
复制PHP内容到剪贴板
PHP代码:
<font face="新宋体">$states <span style="color: rgb(0,119,0)">=array(</span><span style="color: rgb(0,0,187)">‘Ohio’</span><span style="color: rgb(0,119,0)">, </span><span style="color: rgb(0,0,187)">‘New York’</span></font><font face="新宋体"><span style="color: rgb(0,119,0)">);<br>
</span><span style="color: rgb(0,0,187)">$state </span><span style="color: rgb(0,119,0)">= </span><span style="color: rgb(0,0,187)">array_pop</span><span style="color: rgb(0,119,0)">(</span><span style="color: rgb(0,0,187)">$states</span></font><font face="新宋体"><span style="color: rgb(0,119,0)">);<br>
</span><span style="color: rgb(255,128,0)">//output: New York</span><br>
<br>
</font>
array_shift();
与array_pop类似,只不过它移出和返回的是数组的第一个值。数字键名将重新从零开始记数,文字键名则保持不变!
array_unshift();
与array_push类似,只不过是从数组头部插入数据!数字键名将重新从零开始记数,文字键名则保持不变!
array_pad();
array array_pad ( array input, intpad_size, mixed pad_value )
用值将数组填充到指定长度!
pad_size为正,则从右侧开始填补,为负,从左侧开始填补,小与数组长度,则保持数组不变!示例见手册!
Locating array elements
in_array();
检查数组中是否存在某个值,基础,不说了,具体见手册!
array_keys();
array array_keys ( array input [,mixed search_value [, bool strict]] )
返回数组中的所有键名。如果选定search_value,则返回选定的键名!自PHP 5]起,可以用strict参数来进行全等比较(===)。
array_key_exists();
bool array_key_exists ( mixed key,array search )
检查给定的键名或索引是否存在于数组中,存在则返回true。一些更具体的应用记得查看手册!
array_values();
array array_values ( array input )
与array_keys()类似!返回的是数组的所有键值!
array_search();
mixed array_search ( mixed needle,array haystack [, bool strict] )
在 haystack中搜索 needle参数并在找到的情况下返回键名,否则返回FALSE。
如果needle是字符串,则比较区分大小写!
如果strict是true,还得比较类型
如果needle在haystack中出现不止一次,则返回第一个匹配的键。要返回所有匹配值的键,应该用array_keys()加上可选参数search_value来代替!

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

AI Hentai Generator
Generate AI Hentai for free.

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



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

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

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

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

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,

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

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 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.
