Home php教程 php手册 PHP基础 数组函数 的总结

PHP基础 数组函数 的总结

Jun 13, 2016 am 10:17 AM
php function Base Summarize array of

<?<span php
</span><span /*</span><span *
 *  PHP基础 数组操作函数
 *
 * 指针函数:【类似于数据库的游标】 见例1.1
 * current($arr)/pos    返回当前指针指向的元素
 * key($arr)            返回当前指针指向的键
 * next($arr)            返回当前指针指向的 下一个元素
 * prev($arr)            返回当前指针指向的 上一个元素
 * end($arr)            返回当前指针指向的 最后一个元素
 * reset($arr)            将指针重置指向到 第一个元素
 *
 * 其它函数:
 * count($arr,model)/sizeof($arr,model)        获取数组的长度,model=0/COUNT_RECURSIVE:是否检测多维数组,默认值为0不检测[常用]
 * range(val1,val2,[step])    生成一个指定范围的数组 range(0, 40, 10):array(0, 10, 20, 30, 40)
 * list(var1,var2,var3...)    将数组中的值赋给list中的变量
 * key($arr)                返回当前元素的键名
 *
 * 单数组排序函数:
 * sort             对数组升序排序
 * rsort        对数组倒序排序
 * usort         使用用户自定义的比较函数对数组中的值进行升序排序
 * 
 * krsort        对数组按照键名逆向排序
 * ksort        对数组按照键名排序
 * uksort         使用用户自定义的比较函数对数组中的键名进行排序 
 * 
 * asort        对数组进行排序并保持索引关系
 * arsort         对数组进行逆向排序并保持索引关系 
 * uasort         使用用户自定义的比较函数对数组中的值进行排序并保持索引关联 
 * 
 * natcasesort     用&ldquo;自然排序&rdquo;算法对数组进行不区分大小写字母的排序 
 * natsort         用&ldquo;自然排序&rdquo;算法对数组排序,区分大小
 * 
 *
 * 数组的 分割与合并:
 * array_chunk($arr,size,[TRUE/FALSE])        数组分割:将一个数组分割成多个数组
 *                                             size:以几个元素为一组
 *                                            [TRUE/FALSE]是否保留原来的键名,默认为FALSE,索引从0开始
 * array_merge($arr1,$arr2...)                合并数组 
 *                                             如果字符键名重复,则后面的覆盖前面的键,如果是数字键重复,则以连续方式重建索引
 * array_combine ($arrKeys ,$arrValues)        构建新数组 = 键数组 + 值数组 (两个数组不为空且个数相同,否则返回FALSE)
 * 
 *
 * 
 * 使用 回调函数 对数组元素进行 处理:                    
 * array_walk($arr,fn, pms)    R:TRUE/FALSE      对数组中的元素进行处理 (fn:对数组元素处理的函数,pms:fn的参数)R:表示返回值
 * array_filter($arr,fn)                    对数组元素进行过滤    (fn:对数组元素进行判断,返回true,则将元素返回)
 * array_map(fn,$arr1,$arr2...)    R:arr        对数组中的元素进行处理 (fn:对数组元素处理的函数)
 * array_reduce(arr,fn,[value])                用回调函数迭代地将数组简化为单一的值(例:计算一个数组的和/积 value:表示用于处理的第一个元素)
 *
 * 数组的 查找与检测:
 * array_values($arr)    R:arr                      对数组重建数字索引
 * array_key_exists(keyname,$arr) R:TRUE/FALSE  检测key是否存在  isset() 对于数组中为 NULL 的值不会返回 TRUE,而 array_key_exists() 会
 * in_array(value,$arr,[TRUE/FALSE])             检测指定的值是否存在 [TRUE/FALSE]:默认TRUE:区分类型检测
 * array_search(value,$arr,[TRUE/FALSE])        搜索给定的值返回键名否则返回FALSE (使用 === 运算符来测试本函数的返回值)
 *
 *
 * 数组  去重:
 * array_unique($arr)    R:arr             去除重复的元素,返回去重后的数组
 *
 * 数组  填充:
 * array_fill(startIndex,count,value)    用指定的值生成一个数组
 *                                         array_fill(3,5,"aa"):从索引3开始,添加5个aa元素
 * array_pad(input, pad_size, pad_value)值将数组填补到指定长度                             
 *
 * 数组  合并:
 * array_merge_recursive(array1)    递归地合并一个或多个数组
 * array_merge(array1)                合并一个或多个数组
 *
 * 多数组  排序:
 * array_multisort(arr)    对多个数组或多维数组进行排序(默认值 SORT_ASC 和 SORT_REGULAR)
 * array_reverse        对数组进行倒序排序
 * 
 *
 * 数组键/值的 获取和添加:
 * array_shift      从开头的单元移出数组 
 * array_unshift      从开头插入一个或多个单元 
 * array_pop         从未尾将数组最后一个单元弹出(出栈)
 * array_push          从未尾将一个或多个单元压入数组的末尾(入栈) 
 * 
 * array_slice        从数组中取出一段
 * array_splice        把数组中的一部分去掉并用其它值取代 
 * array_search     在数组中搜索给定的值,如果成功则返回相应的键名 
 * array_rand        从数组中随机取出一个或多个单元 
 * array_keys($arr,[keyname],[value]) R:keys    获取数组中的键 [keyname]:指定的键名 [value]:与键名进行全等比较的值
 *
 * 
 * 数组中元素和键的 统计与计算:
 * array_sum                                 计算数组中所有值的和 
 * array_product                            计算数组中所有值的乘积
 * array_count_values ($arr) R:newArr([value]=>times)        统计元素在数组中出现的次数    
 *
 * 其它:
 * array_change_key_case ($arr,[caseTye])    键名转换:将字符类型的键转换为全大写或小写,
 *                                             可选参数:[caseTye]=CASE_UPPER/CASE_LOWER(默认为CASE_LOWER)
 *
 *
 * 
 </span><span */</span>



<span //</span><span 例1.1</span>
<span $arrNums</span> = <span array</span>(2,5,8,9<span );
</span><span echo</span> <span current</span>(<span $arrNums</span>)."\n";<span //</span><span 2    </span>
<span echo</span> <span key</span>(<span $arrNums</span>)."\n";    <span //</span><span 0</span>
<span echo</span> <span next</span>(<span $arrNums</span>)."\n";    <span //</span><span 5</span>
<span echo</span> <span prev</span>(<span $arrNums</span>)."\n";    <span //</span><span 2</span>
<span echo</span> <span end</span>(<span $arrNums</span>)."\n";    <span //</span><span 9</span>
<span echo</span> <span reset</span>(<span $arrNums</span>)."\n";    <span //</span><span 2</span>

?>
Copy after login

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