Home php教程 php手册 PHP常用函数总结(数组部分)共享版会有些错误

PHP常用函数总结(数组部分)共享版会有些错误

Jun 13, 2016 am 10:46 AM
php and shared function Commonly used Summarize operate array Format generate use change enter output part mistake

 

函数名 用途 格式 输入 输出 操作

数组生成与转化:

array() 生成一个数组array array(mixed [...]) 数组值或,键=>值 一个数组型变量 无

array_combine() 生成一个数组,用一个数组的值作为键名,另一个数组值作为值array array_combine(array $keys,array $values) $keys为提供键的数组,$values为提供值的数组 合成的数组 无

range() 建立一个指定范围单元的数组array range(mix $low,mix $high,[num $step]) $low最小值,$high最大值,$step步长 合成的数组 无

compact() 建立一个数组,包括他们的变量名和值arr compact(mix $varname,[,mix $...]) 各种变量,和数组 返回由变量名为键,变量值为值的数组,变量也可以为多维数组.会递归处理 无

array_fill() 用给定值生成数组array array_fill(int $start,int $num,mix $value) $start的键开始,$num个元素,$value为填充内容 返回完成的数组 无

数组合并和拆分:

array_chuk() 将一个数组分成指定多个array array_chunk(arr $input,int $size[,bool $preserve_keys]) $input被操作数组,$size每份含几个元素,$preserve_keys布尔型,是否保留键 分割后的多维数组 无

array_merge() 合并一个或多个数组.如果元素的键相同,则前面的将被覆盖, 索引的序列不会覆盖,只会自动增加arr array_merge(arr $arr1[,arr $arr2,...]) $arr1等为被操作数组 返回完成的数组 无

数组比较操作:

array_diff() 计算数组的差集array array_diff(arr $arr1,arr $arr2[,...]) 输入两个或多个数组 比较几个数组元素值的差集 无

array_intersect() 计算数组的交集array array_intersect(arr $arr1,arr $arr2[,...]) 输入两个或多个数组 比较几个数组元素值的交集 无

 

数组查找替换操作:

array_search() 从数组中查给定值返回键名mix array_search(mix $need,arr $arr[bool $stric]) $need为查找字符串,$arr为目标数组,$stric为ture则同时还要匹配变量类型 成功返回键名,失败返回false 无

array_slice() 从数组中取出一段arr array_slice(arr $array,int $offset[,int $length[bool $p_key]) $array目标数组;$offset偏移量;$length截取长度;$p_key:true则保留键名.默认会重新给定键名$array目标数组;$offset非负则从此偏移量开始,如为负值则从距离末端这么远开始;$length为正,则序列中将有这么多单元, 如果未负,则序列截止在距离末端这么远的位置. 无

array_splice() 把数组中一部分去掉用其他值替代arry array_splice(arr &$input,int $offset[,int $leng[,arr $rep]]) $input为目标数组,$offset为偏移量,$leng没有则移除从$offset到结尾所有元素,为正值则移除这么多单元,为负值则移除从$offk到结尾的$leng这么多单元 被替换出来的部分 将原数组替换,注意替换后数组中键名不保留,如果给出$rep则原数组中会被新数组元素替换

array_sum() 计算数组中所有数值的和number array_sum(arr $array) 目标数组 返回求和 无

in_array() 查找数组中是否存在某个值,区分大小写bool in_array(mix $need,arr $array[,bool $str]) 在$array中搜索$need,如果$str为true,则检查类型也要匹配. 找到就返回true,没有就返回false 无

array_key_exists() 检查给定键名是否存在bool array_key_exists(mix $key,arr $serch) $key查找的键,$serch目标数组 返回是否查到 无

数组指针操作:

key()

current() 别名pos()

next()

prev()

end()

reset()

list() 把数组中的值赋给变量void list(mix $var1[,$var2...])=arr $array

array_shift()

array_unshift()

array_push() 数组最后压入多个元素int array_push(arr &$array,mix $var1[,$var2...]) &$array目标数组,$var1..为加入值 返回1为成功,0为失败 直接对目标数组操作

array_pop() 数组最后弹出一个元素mix array_pop(arr &$array) &$array为目标数组 返回被弹出的元素值 直接对目标数组操作

数组键值操作:

shuffle() 将数组打乱,保留键名bool shuffle(arr &$array) 输入数组 返回true 打乱顺序后键名不会有变化

count()

array_flip() 交换数组中的键和值arr array_flip(arr $trans) $trans被操作数组 返回完成的数组 无

array_keys() 返回数组所有的键,组成一个数组arr array_keys(arr $input[,mix $search[,bool $str]]) $search有值则只返回这个值对应的键名,$str为true则,同时还要比较类型 返回键名组成的数组 无

array_values() 返回数组中所有值,组成一个数组array array_values ( array $input )

array_reverse() 返回一个元素顺序相反的数组arr array_reverse(arr $input[,bool $key]) 输入目标数组,$key为true则保留原有键名 顺序相反的一个数组 无

array_count_values() 统计数组中所有的值出现的次数array array_count_values(array $input) $input是被操作数组 用input的值做键,出现次数做值的数组 无

array_rand() 从数组中随机抽取一个或多个元素,注意是键名!!! mix array_range(arr $input[,int $num]) $input为目标数组,$num为抽取几个元素 返回被抽取元素的键,如果为一个则就是个键名;多个元素,则会有这些键作为值组成索引数组返回 无

each()

array_unique() 移除数组中的重复值,先对值做排序,然后对值只保留第一个遇到的键名,接着忽略其余键名array array_unique(arr $array) 输入目标数组 键名保留不变的返回无重复值数组 无

对数组进行排序:

sort() 对数组排序(由低到高),不保留键名bool sort(arr &$array[,int $flag]) 目标数组 成功返回true,失败返回false 对数组元素进行重排,同时改变键名

rsort() 对数组逆向排序(由高到低),不保留键名bool rsort(arr &$array[,int $flag]) 目标数组 成功返回true,失败返回false 对数组元素进行重排,同时改变键名

asort() 对数组排序,保持索引关系bool asort(arr &$array[,int $flag]) 目标数组 成功返回true,失败返回false 对数组进行排序,保留原来的索引或键

arsort() 对数组逆向排序,保持索引关系

ksort() 对数组按照键名排序bool ksort(arr &$array[int $flag]) 目标数组 成功返回true,失败返回false 对键名排序,保留键值对应关系

krsort() 对数组按照键名逆向排序

natsort() """自然算法""对数组进行排序,保持键值关系" bool natsort(arr &$array) 目标数组 成功返回true,失败返回false 对值进行自然排序,保留键值对应关系

natcasesort() 自然排序,不区分大小写bool natcasesort(arr &$array) 目标数组 成功返回true,失败返回false 不区分大小写的对值进行自然法排序,保持键值对应关系

 

摘自 战地日记_LAMP兄弟连

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

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

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