Home php教程 php手册 PHP操作数组相关函数

PHP操作数组相关函数

Jun 13, 2016 pm 12:11 PM
php range step for value function create operate array of Related order

ange($low, $high),range($low, $high, $step);//创建顺序值的数组如:range(1,4)为(1,2,3,4)又如range('a','z')

each($array)按顺序返回数组的当前元素,并且将下一个元素设置为当前元素;

reset($array)将数组当前元素重新设置到数组开始处

list()可以用来将一个数组分解为一系列的值,如 list($a,$b)=each($array)

shuffle($array),array_rand($arg, $num_req);对数组随机排序

array_reverse($input),array_reverse($input, $preserve_keys) 返回原数组的反向排序

sort($array);对数组排序



PHP数组是一个重要的概念,它包含有大量的函数,方便人们的开发…现将它的数组分类,以方便查询及应用.
先说说PHP数组的定义…PHP数组包含两个项,key和value,可以通过key来获取相应的value,其中key又可以是数值和关联的,如$array[0],$array[one]…
创建数组
PHP中的数组声明跟其它语言的也有点小小的差别,但一样可以声明为一维,两维,三维及多维等,如
$array[0] = 1,$array = array(1,2,3); 一维数组,只包括三个值,属于数值型数组,引用时可用$array[0]来代表1,创建数值数组时可以省略索引.

复制代码 代码如下:


$array = array(
1 => “one”,
2 => “two”,
3 => “three”,
4 => array(
“one” => 1,
“two” => 2,
“three” => 3
)
);


二维数组,同时又是关联数组,引用时可以$array[4][“one”]来代表1.
三维以上依此类推…
如果要批量创建数组,则可以通过下面这个函数:
array range ( mixed low, mixed high [, number step] )
如$array = range(1,6);代表array(1,2,3,4,5,6);
$array = range(a,f); 代表 array(a,b,c,d,e,f);

输出数组
PHP中输出数组的函数有比较多,常用的有
bool print_r ( mixed expression [, bool return] )
void var_dump ( mixed expression [, mixed expression [, ...]] )
还有像echo,print,printf都可以输出单个数组.

测试数组
有时我们需要判定一个变量是否为数组,则可以使用:
bool is_array ( mixed var )

增加或删除数组元素
数组声明后并不是一成不变的,可能通过对数组的增加删除来进行深入的操作:
int array_push ( array &array, mixed var [, mixed ...] ) 将一个或多个单元压入数组的末尾,数组的长度根据入栈变量的数目增加,如array_push($array,$var)
mixed array_pop ( array &array ) 将数组的最后一个元素弹出(出栈),并在结束后重置数组的指针
mixed array_shift ( array &array ) 返回数组的第一个元素.
int array_unshift ( array &array, mixed var [, mixed ...] ) 在数组的开头插入一个或多个单元
array array_pad ( array input, int pad_size, mixed pad_value ) 用值将数组填补到指定的长度,如array_pad($array,3,$var);

定位数组元素
bool in_array ( mixed needle, array haystack [, bool strict] ) 检查数组中是否存在某个值
array array_keys ( array input [, mixed search_value [, bool strict]] ) 返回数组中的所有键名,重组成一个新数组
bool array_key_exists ( mixed key, array search ) 检查给定的key是否存在于数组中.
array array_values ( array input ) 返回数组中所有的值
mixed array_search ( mixed needle, array haystack [, bool strict] ) 在数组中搜索给定的值,成功则返回key.

遍历数组
PHP中提供了很多获取key和value的函数
mixed key ( array &array ) 从关联数组中取得键名
mixed reset ( array &array ) 将数组指针重置
array each ( array &array ) 返回数组中的键/值对并将数组向前移一步
mixed current ( array &array ) 返回数组中的当前单元
mixed end ( array &array ) 将数组中的指针移向最后一位
mixed next ( array &array ) 将数组中的指针移向下一位
mixed prev ( array &array ) 将数组中的指针移向上一位
array array_reverse ( array array [, bool preserve_keys] ) 返回一个单元顺序相反的数组
array array_flip ( array trans ) 将数组中的键值角色调换
除了上面的函数外还可以使用循环来对数组中的元素进行遍历,如
foreach (array_expr as $value)
{ statement }
foreach (array_expr as $key=>$value)
{ statement }
提取每个键/值对,直到获得所有项或满足某些内部条件为止
void list ( mixed varname, mixed ... ) 把数组中的值赋给一些变量

确定数组大小和唯一性
int count ( mixed var [, int mode] ) 计算数组中单元数组或对象中属性的个数, sizeof 的同名函数
array array_count_values ( array input ) 统计数组中所有值出现的次数
array array_unique ( array array ) 移除数组中重复的值

数组排序
这个听说是计算器的核心问题…呵呵…事实也是这样…
bool sort ( array &array [, int sort_flags] ) 对数组进行排序
bool natsort ( array &array ) 用自然排序法对数组进行排序
bool natcasesort ( array &array ) 用自然排序法对数组进行排序,不区分大小写
bool rsort ( array &array [, int sort_flags] ) 对数组进行逆向排序
bool asort ( array &array [, int sort_flags] ) 对数组进行排序并保持索引关系
bool array_multisort ( array ar1 [, mixed arg [, mixed ... [, array ...]]] ) 对多个数组或多维数组进行排序
bool arsort ( array &array [, int sort_flags] ) 对数组进行逆序排序并保持索引关系
bool ksort ( array &array [, int sort_flags] ) 对数组按键名排序
bool krsort ( array &array [, int sort_flags] ) 对数组按键名逆序排序

合并,拆分,接合和分解数组
array array_combine ( array keys, array values ) 创建一个数组,一个数组的值作为其键名,另一个数组的值作为其值
array array_merge ( array array1 [, array array2 [, array ...]] ) 合并一个或多个数组
array array_merge_recursive ( array array1 [, array ...] ) 递归地全部一个或多个数组
array array_slice ( array array, int offset [, int length [, bool preserve_keys]] ) 从数组中取出一段,建立一个新的数组,如果offset为正数,拆分从距数组开关的offset位置开始,如果为负数,则拆分从距数组末尾的offset 位置开始,此时距数组开关的count(input_array)-|length|位置结束
array array_splice ( array &input, int offset [, int length [, array replacement]] ) 把数组中的部分值去掉,并用其它值替代.offset设置同上
array array_intersect ( array array1, array array2 [, array ...] ) 计算数组的交集,即是说如果第一个数组中出现过的值在接下来的几个数组中都有出现,则取出该值
array array_intersect_assoc ( array array1, array array2 [, array ...] ) 带索引检查数组中的交集
array array_intersect_key ( array array1, array array2 [, array ...] ) 使用键名比较数组中的交集
array array_diff ( array array1, array array2 [, array ...] ) 计算数组的差集, 即是说跟第一个数组中不同的值
array array_diff_assoc ( array array1, array array2 [, array ...] ) 带索引检查数组中的差集
array array_diff_key ( array array1, array array2 [, array ...] ) 使用键名比较数组中的差集

其它比较有用的数组函数
数组函数还有好多没有列出来…再上几个比较有用也比较常的,其它的就参考手册啦…手册里很清楚
mixed array_rand ( array input [, int num_req] ) 数组中随机取出一个或多个键,num指定个数
bool shuffle ( array &array ) 将数组打乱
number array_sum ( array array ) 计算数组中所有值的总和,关联数组忽略
array array_chunk ( array input, int size [, bool preserve_keys] ) 将一个数组分割成几个
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

Video Face Swap

Video Face Swap

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

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

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

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,

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

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