search
HomeBackend DevelopmentPHP TutorialSummary of common functions for PHP arrays

Summary of common functions for PHP arrays

Sep 25, 2020 pm 03:50 PM
phpfunctionarray

Summary of common functions for PHP arrays

1. Basic functions of array operations

数组的键名和值
array_values($arr);  获得数组的值
array_keys($arr);  获得数组的键名
array_flip($arr);  数组中的值与键名互换(如果有重复前面的会被后面的覆盖)
in_array("apple",$arr);  在数组中检索apple
array_search("apple",$arr);  在数组中检索apple ,如果存在返回键名
array_key_exists("apple",$arr);  检索给定的键名是否存在数组中
isset($arr[apple]):   检索给定的键名是否存在数组中

数组的内部指针
current($arr);  返回数组中的当前单元
pos($arr);  返回数组中的当前单元
key($arr);  返回数组中当前单元的键名
prev($arr);  将数组中的内部指针倒回一位
next($arr);  将数组中的内部指针向前移动一位
end($arr);  将数组中的内部指针指向最后一个单元
reset($arr;  将数组中的内部指针指向第一个单元
each($arr);  将返回数组当前元素的一个键名/值的构造数组,并使数组指针向前移动一位
list($key,$value)=each($arr);  获得数组当前元素的键名和值

数组和变量之间的转换
extract($arr);用于把数组中的元素转换成变量导入到当前文件中,键名当作变量名,值作为变量值
注:(第二个参数很重要,可以看手册使用)使用方法 echo $a;
compact(var1,var2,var3);用给定的变量名创建一个数组

2. Array segmentation and filling

数组的分段
array_slice($arr,0,3);  可以将数组中的一段取出,此函数忽略键名
array_splice($arr,0,3,array("black","maroon"));  可以将数组中的一段取出,与上个函数不同在于返回的序列从原数组中删除

分割多个数组
array_chunk($arr,3,TRUE);  可以将一个数组分割成多个,TRUE为保留原数组的键名

数组的填充
array_pad($arr,5,'x');  将一个数组填补到制定长度

3. Array and stack

array_push($arr,"apple","pear");  将一个或多个元素压入数组栈的末尾(入栈),返回入栈元素的个数
array_pop($arr);  将数组栈的最后一个元素弹出(出栈)

4. Array and queue

array_shift($arr);数组中的第一个元素移出并作为结果返回(数组长度减1,其他元素向前移动一位,数字键名改为从零技术,文字键名不变)
array_unshift($arr,"a",array(1,2));在数组的开头插入一个或多个元素

5. Callback function

array_walk($arr,'function','words');  使用用户函数对数组中的每个成员进行处理(第三个参数传递给回调函数function)
array_mpa("function",$arr1,$arr2);  可以处理多个数组(当使用两个或更多数组时,他们的长度应该相同)
array_filter($arr,"function");  使用回调函数过滤数组中的每个元素,如果回调函数为TRUE,数组的当前元素会被包含在返回的结果数组中,数组的键名保留不变
array_reduce($arr,"function","*");  转化为单值函数(*为数组的第一个值)

6. Sorting of arrays

通过元素值对数组排序
sort($arr);  由小到大的顺序排序(第二个参数为按什么方式排序)忽略键名的数组排序
rsort($arr);  由大到小的顺序排序(第二个参数为按什么方式排序)忽略键名的数组排序
usort($arr,"function");  使用用户自定义的比较函数对数组中的值进行排序(function中有两个参数,0表示相等,正数表示第一个大于第二个,负数表示第一个小于第二个)忽略键名的数组排序
asort($arr);  由小到大的顺序排序(第二个参数为按什么方式排序)保留键名的数组排序
arsort($arr);  由大到小的顺序排序(第二个参数为按什么方式排序)保留键名的数组排序
uasort($arr,"function");  使用用户自定义的比较函数对数组中的值进行排序(function中有两个参数,0表示相等,正数表示第一个大于第二个,负数表示第一个小于第二个)保留键名的数组排序

通过键名对数组排序
ksort($arr);  按照键名正序排序
krsort($arr);  按照键名逆序排序
uksort($arr,"function");  使用用户自定义的比较函数对数组中的键名进行排序(function中有两个参数,0表示相等,正数表示第一个大于第二个,负数表示第一个小于第二个)

自然排序法排序
natsort($arr);  自然排序(忽略键名)
natcasesort($arr);  自然排序(忽略大小写,忽略键名)

7. Calculation of arrays

数组元素的求和
array_sum($arr);  对数组内部的所有元素做求和运算

数组的合并
array_merge($arr1,$arr2);  合并两个或多个数组(相同的字符串键名,后面的覆盖前面的,相同的数字键名,后面的不会做覆盖操作,而是附加到后面)
“+”$arr1+$arr2;  对于相同的键名只保留后一个
array_merge_recursive($arr1,$arr2);   递归合并操作,如果数组中有相同的字符串键名,这些值将被合并到一个数组中去。如果一个值本身是一个数组,将按照相应的键名把它合并为另一个数组。当数组 具有相同的数组键名时,后一个值将不会覆盖原来的值,而是附加到后面

数组的差集
array_diff($arr1,$arr2);  返回差集结果数组
array_diff_assoc($arr1,$arr2,$arr3);  返回差集结果数组,键名也做比较

数组的交集
array_intersect($arr1,$arr2);  返回交集结果数组
array_intersect_assoc($arr1,$arr2);  返回交集结果数组,键名也做比较

8. Other arrays Function

range(0,12);  创建一个包含指定范围单元的数组
array_unique($arr);  移除数组中重复的值,新的数组中会保留原始的键名
array_reverse($arr,TRUE);  返回一个单元顺序与原数组相反的数组,如果第二个参数为TRUE保留原来的键名
//srand((float)microtime()*10000000);   随机种子触发器
array_rand($arr,2);  从数组中随机取出一个或 多个元素
shuffle($arr);  将数组的顺序打乱

For more programming-related knowledge, please visit: Introduction to Programming! !

The above is the detailed content of Summary of common functions for PHP arrays. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:phpxs. If there is any infringement, please contact admin@php.cn delete
PHP: An Introduction to the Server-Side Scripting LanguagePHP: An Introduction to the Server-Side Scripting LanguageApr 16, 2025 am 12:18 AM

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP and the Web: Exploring its Long-Term ImpactPHP and the Web: Exploring its Long-Term ImpactApr 16, 2025 am 12:17 AM

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

Why Use PHP? Advantages and Benefits ExplainedWhy Use PHP? Advantages and Benefits ExplainedApr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

Debunking the Myths: Is PHP Really a Dead Language?Debunking the Myths: Is PHP Really a Dead Language?Apr 16, 2025 am 12:15 AM

PHP is not dead. 1) The PHP community actively solves performance and security issues, and PHP7.x improves performance. 2) PHP is suitable for modern web development and is widely used in large websites. 3) PHP is easy to learn and the server performs well, but the type system is not as strict as static languages. 4) PHP is still important in the fields of content management and e-commerce, and the ecosystem continues to evolve. 5) Optimize performance through OPcache and APC, and use OOP and design patterns to improve code quality.

The PHP vs. Python Debate: Which is Better?The PHP vs. Python Debate: Which is Better?Apr 16, 2025 am 12:03 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on the project requirements. 1) PHP is suitable for web development, easy to learn, rich community resources, but the syntax is not modern enough, and performance and security need to be paid attention to. 2) Python is suitable for data science and machine learning, with concise syntax and easy to learn, but there are bottlenecks in execution speed and memory management.

PHP's Purpose: Building Dynamic WebsitesPHP's Purpose: Building Dynamic WebsitesApr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP: Handling Databases and Server-Side LogicPHP: Handling Databases and Server-Side LogicApr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

How do you prevent SQL Injection in PHP? (Prepared statements, PDO)How do you prevent SQL Injection in PHP? (Prepared statements, PDO)Apr 15, 2025 am 12:15 AM

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

See all articles

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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor