Home php教程 php手册 php array相关函数个人小结

php array相关函数个人小结

Jun 13, 2016 am 10:58 AM
array php personal for function Split Bundle number array Related


1.array_chunk() 把一个数组分割为新的数组块。其中每个数组的单元数目由 size 参数决定。最后一个数组的单元数目可能会少几个。
例子 
$a=array("a"=>"Cat","b"=>"Dog","c"=>"Horse","d"=>"Cow");
print_r(array_chunk($a,2));
?>
输出:
Array (
[0] => Array ( [0] => Cat [1] => Dog )
[1] => Array ( [0] => Horse [1] => Cow )
)

这里非常像linux里的split工具。
[root@xen187v tmp]$ cat tmp
1
2
3
4
5
6
7
[root@xen187v tmp]$ split -l 2 tmp
[root@xen187v tmp]$ ls
tmp  xaa  xab  xac  xad
[root@xen187v tmp]$ cat xaa
1
2
[root@xen187v tmp]$ cat xab
3
4
[root@xen187v tmp]$ cat xac
5
6
[root@xen187v tmp]$ cat xad
7


2.
array_merge() 把一个或多个数组合并为一个数组。【这个是纵向合并】
array_combine() 函数通过合并两个数组来创建一个新数组,其中的一个数组是键名,另一个数组的值为键值。【这个是横向合并】
如果其中一个数组为空,或者两个数组的元素个数不同,则该函数返回 false。
例子
$a1=array("a","b","c","d");
$a2=array("Cat","Dog","Horse","Cow");
print_r(array_combine($a1,$a2));
?>

这个很像linux下的paste命令类似。
paste单词意思是粘贴。该命令主要用来将多个文件的内容合并,与cut命令完成的功能刚好相反。


粘贴两个不同来源的数据时,首先需将其分类,并确保两个文件行数相同
[root@xen187v tmp]$ cat xaa
1
2
[root@xen187v tmp]$ cat xab
3
4
[root@xen187v tmp]$ paste xaa xab
1       3
2       4
给xaa多加一行看看会怎么样
[root@xen187v tmp]$ cat xaa
1
2
3
[root@xen187v tmp]$ paste xaa xab
1       3
2       4
3
给xab再加两行看看怎样样
[root@xen187v tmp]$ cat xab
i
i
3
4
[root@xen187v tmp]$ paste xaa xab
1       i
2       i
3       3
        4
[root@xen187v tmp]$



3.
array_sum() 计算数组中所有值的和。
array_count_values() 函数用于统计数组中所有值出现的次数。
本函数返回一个数组,其元素的键名是原数组的值,键值是该值在原数组中出现的次数。
【很像uniq -c
[root@xen187v tmp]$ cat xab
i
i
3
4
[root@xen187v tmp]$ uniq -c xab
      2 i
      1 3
      1 4
[root@xen187v tmp]$ uniq -c xab|awk '{print $2" "$1}'
i 2
3 1
4 1
[root@xen187v tmp]$

4.


【感慨:要是这些数组函数名和linux命令名一致该多好,方便记忆】
5.array_diff() 函数返回第一个数组,不在后继数组中的数据项数组
6.array_flip()交换数组中的键和值. 函数返回一个反转后的数组,如果同一值出现了多次,则最后一个键名将作为它的值,所有其他的键名都将丢失。
如果原数组中的值的数据类型不是字符串或整数,函数将报错。
【这个值得记忆,处理数据时,容易遇到key->value要翻转的情况】
7.array_intersect() 计算数组的交集。

面试中容易出的题,用原生代码求两个数组的交集
function intersectArray($arr1,$arr2)
{
$tmpArr = array();
foreach($arr1 as $v1) $tmpArr[$v1] = 0;
foreach($arr2 as $v2)
{
if(isset($tmpArr[$v2])
{
$tmpArr[$v2] = 1;
}
}
//$tmpArr中值为1的就是交集
$retArr = array();
foreach($tmpArr as $key => $v)
{
if($v == 1) $retArr[] = $key;
}
return $retArr;
}



8.array_keys() 返回数组中所有的键名。
9.
array_rand() 从数组中随机选出一个或多个元素,并返回。
shuffle() 函数把数组中的元素按随机顺序重新排列
10.
array_reverse() 将原数组中的元素顺序翻转,创建新的数组并返回。
11.
array_search() 在数组中搜索给定的值,如果成功则返回相应的键名。
12
array_unique() 删除数组中重复的值。
13
arsort() 对数组进行逆向排序并保持索引关系。
asort() 对数组进行排序并保持索引关系。
krsort() 对数组按照键名逆向排序。
ksort() 对数组按照键名排序。
 

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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1246
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

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 and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles