PHP Array函数分类
一、 数组操作的基本函数
1、数组的键名和值
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]): 检索给定的键名是否存在数组中
2、数组的内部指针
current($arr); 返回数组中的当前单元
pos($arr); 返回数组中的当前单元
key($arr); 返回数组中当前单元的键名
prev($arr); 将数组中的内部指针倒回一位
next($arr); 将数组中的内部指针向前移动一位
end($arr); 将数组中的内部指针指向最后一个单元
reset($arr; 将数组中的内部指针指向第一个单元
each($arr); 将返回数组当前元素的一个键名/值的构造数组,并使数组指针向前移动一位
list($key,$value)=each($arr); 获得数组当前元素的键名和值
3、数组和变量之间的转换
extract($arr); 用于把数组中的元素转换成变量导入到当前文件中,键名当作变量名,值作为变量值
compact(var1,var2,var3);用给定的变量名创建一个数组
二、数组的分段和填充
1、数组的分段
array_slice($arr,0,3); 可以将数组中的一段取出,此函数忽略键名
array_splice($arr,0,3,array("black","maroon")); 可以将数组中的一段取出,与上个函数不同在于返回的序列从原数组中删除
2、分割多个数组
array_chunk($arr,3,TRUE); 可以将一个数组分割成多个,TRUE为保留原数组的键名
3、数组的填充
array_pad($arr,5,'x'); 将一个数组填补到制定长度
三、数组与栈
array_push($arr,"apple","pear"); 将一个或多个元素压入数组栈的末尾(入栈),返回入栈元素的个数
array_pop($arr); 将数组栈的最后一个元素弹出(出栈)
四、数组与列队
array_shift($arr);数组中的第一个元素移出并作为结果返回(数组长度减1,其他元素向前移动一位,数字键名改为从零技术,文字键名不变)
array_unshift($arr,"a",array(1,2));在数组的开头插入一个或多个元素
五、回调函数
array_walk($arr,'function','words'); 使用用户函数对数组中的每个成员进行处理(第三个参数传递给回调函数function)
array_map("function",$arr1,$arr2); 可以处理多个数组(当使用两个或更多数组时,他们的长度应该相同)
array_filter($arr,"function"); 使用回调函数过滤数组中的每个元素,如果回调函数为TRUE,数组的当前元素会被包含在返回的结果数组中,数组的键名保留不变
array_reduce($arr,"function","*"); 转化为单值函数(*为数组的第一个值)
六、数组的排序
1、通过元素值对数组排序
sort($arr); 由小到大的顺序排序(第二个参数为按什么方式排序)忽略键名的数组排序
rsort($arr); 由大到小的顺序排序(第二个参数为按什么方式排序)忽略键名的数组排序
usort($arr,"function"); 使用用户自定义的比较函数对数组中的值进行排序(function中有两个参数,0表示相等,正数表示第一个大于第二个,负数表示第一个小于第二个)忽略键名的数组排序
asort($arr); 由小到大的顺序排序(第二个参数为按什么方式排序)保留键名的数组排序
arsort($arr); 由大到小的顺序排序(第二个参数为按什么方式排序)保留键名的数组排序
uasort($arr,"function"); 使用用户自定义的比较函数对数组中的值进行排序(function中有两个参数,0表示相等,正数表示第一个大于第二个,负数表示第一个小于第二个)保留键名的数组排序
2、通过键名对数组排序
ksort($arr); 按照键名正序排序
krsort($arr); 按照键名逆序排序
uksort($arr,"function"); 使用用户自定义的比较函数对数组中的键名进行排序(function中有两个参数,0表示相等,正数表示第一个大于第二个,负数表示第一个小于第二个)
3、自然排序法排序
natsort($arr); 自然排序(忽略键名)
natcasesort($arr); 自然排序(忽略大小写,忽略键名)
七、数组的计算
1、数组元素的求和
array_sum($arr); 对数组内部的所有元素做求和运算
2、数组的合并
array_merge($arr1,$arr2); 合并两个或多个数组(相同的字符串键名,后面的覆盖前面的,相同的数字键名,后面的不会做覆盖操作,而是附加到后面)“+”$arr1+$arr2; 对于相同的键名只保留后一个
array_merge_recursive($arr1,$arr2); 递归合并操作,如果数组中有相同的字符串键名,这些值将被合并到一个数组中去。如果一个值本身是一个数组,将按照相应的键名把它合并为另一个数组。当数组 具有相同的数组键名时,后一个值将不会覆盖原来的值,而是附加到后面
3、数组的差集
array_diff($arr1,$arr2); 返回差集结果数组
array_diff_assoc($arr1,$arr2,$arr3); 返回差集结果数组,键名也做比较
4、数组的交集
array_intersect($arr1,$arr2); 返回交集结果数组
array_intersect_assoc($arr1,$arr2); 返回交集结果数组,键名也做比较
八、其他的数组函数
range(0,12); 创建一个包含指定范围单元的数组
array_unique($arr); 移除数组中重复的值,新的数组中会保留原始的键名
array_reverse($arr,TRUE); 返回一个单元顺序与原数组相反的数组,如果第二个参数为TRUE保留原来的键名
//srand((float)microtime()*10000000); 随机种子触发器
array_rand($arr,2); 从数组中随机取出一个或 多个元素
shuffle($arr); 将数组的顺序打乱

PHP remains a powerful and widely used tool in modern programming, especially in the field of web development. 1) PHP is easy to use and seamlessly integrated with databases, and is the first choice for many developers. 2) It supports dynamic content generation and object-oriented programming, suitable for quickly creating and maintaining websites. 3) PHP's performance can be improved by caching and optimizing database queries, and its extensive community and rich ecosystem make it still important in today's technology stack.

In PHP, weak references are implemented through the WeakReference class and will not prevent the garbage collector from reclaiming objects. Weak references are suitable for scenarios such as caching systems and event listeners. It should be noted that it cannot guarantee the survival of objects and that garbage collection may be delayed.

The \_\_invoke method allows objects to be called like functions. 1. Define the \_\_invoke method so that the object can be called. 2. When using the $obj(...) syntax, PHP will execute the \_\_invoke method. 3. Suitable for scenarios such as logging and calculator, improving code flexibility and readability.

Fibers was introduced in PHP8.1, improving concurrent processing capabilities. 1) Fibers is a lightweight concurrency model similar to coroutines. 2) They allow developers to manually control the execution flow of tasks and are suitable for handling I/O-intensive tasks. 3) Using Fibers can write more efficient and responsive code.

The PHP community provides rich resources and support to help developers grow. 1) Resources include official documentation, tutorials, blogs and open source projects such as Laravel and Symfony. 2) Support can be obtained through StackOverflow, Reddit and Slack channels. 3) Development trends can be learned by following RFC. 4) Integration into the community can be achieved through active participation, contribution to code and learning sharing.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.


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

SublimeText3 Mac version
God-level code editing software (SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function