php 数组的常用函数
在php教程中数组是种强大的数据类型,他可以做的事情很多,可以存储不同的数据类型在一个数组中,下面我们列出了数组常用的操作,排序,键名对数组排序等做法。
/* 数组的常用函数
*
* 数组的排序函数
* sort()
* rsort()
* usort()
* asort()
* arsort()
* uasort()
* ksort()
* krsort()
* uksort()
* uatsort()
* natcasesort()
* array_multisort()
*
* 1.简单的数组排序
* sort() rsort()
* 2.根据键名对数组排序
* ksort() krsort()
* 3.根据元素的值对数组排序
* asort() arsort()
* 4.根据"自然数排序“法对数组排序
* natsort()//区分大小写字母比较 natcasescort()//不区分大小写字母的比较
* 5.根据用户自定义规则对数组排序
* usort() uasort() uksort()对键排序
* 6.对维数组的排序
* array_multisort()
*
* 拆分、合并、分解、接合的数组函数
* 1.array_slice()
* 2.array_splice()//删除
* 3.array_combine()//合并
* 4.array_merge();//合并
* 5.array_intersect();//多个数组的交集
* 6.array_diff();//返回多个数组的差集
*
* 数组与数据结构的函数
* 1.使用数组实现堆栈 //先进后出
* array_push() array_pop()
* 2.使用数组实现队列 //先进先出
* array_unshift() array_shift() unset()
*
*
* 其他与数组操作有关的函数
* array_rand()
* shuffle()
* array_sum()
* range()
*///简单数组排序的使用
$data=array(5,8,1,7,2);
sort($data);//元素由小到大进行排序
print_r($data);//Array ( [0] => 1 [1] => 2 [2] => 5 [3] => 7 [4] => 8 )
rsort($data);//元素由大到小进行排序
print_r($data);//Array ( [0] => 8 [1] => 7 [2] => 5 [3] => 2 [4] => 1 )//根据键名排序的例子
$data_2=array(5=>"five",8=>"eight",1=>"one",7=>"seven",2=>"two");
ksort($data_2);//对数组的下标进行由小到大排序
print_r($data_2);//Array ( [1] => one [2] => two [5] => five [7] => seven [8] => eight )
krsort($data_2);//对数组的下标进行由大到小排序
print_r($data_2);//Array ( [8] => eight [7] => seven [5] => five [2] => two [1] => one )
//根据元素的值对数组排序
$data_3=array("1"=>"Linux","a"=>"Apache","m"=>"MySQL","l"=>"PHP");
//asort() arsort 与 sort() rsort()的区别在于 前者排序后保持原有的键名,后者不保持原有键名,且键名从0开始
asort($data_3);
print_r($data_3);//Array ( [a] => Apache [1] => Linux [m] => MySQL [l] => PHP )
echo '
';
arsort($data_3);
print_r($data_3);//Array ( [l] => PHP [m] => MySQL [1] => Linux [a] => Apache )
echo '
';
sort($data_3);
print_r($data_3);//Array ( [0] => Apache [1] => Linux [2] => MySQL [3] => PHP )
echo '
';
rsort($data_3);
print_r($data_3);//Array ( [0] => PHP [1] => MySQL [2] => Linux [3] => Apache )//根据”自然数排序法“对数组排序(0-9短者优先)
$data_4=array("file.txt","file11.txt","file2.txt","file22.txt");
sort($data_4);
print_r($data_4);//Array ( [0] => file.txt [1] => file11.txt [2] => file2.txt [3] => file22.txt )
echo '
';
natsort($data_4);
print_r($data_4);//Array ( [0] => file.txt [2] => file2.txt [1] => file11.txt [3] => file22.txt )
echo '
';
natcasesort($data_4);
print_r($data_4);//Array ( [0] => file.txt [2] => file2.txt [1] => file11.txt [3] => file22.txt )
echo '
';//用户自定义排序函数
echo '
';
$data_5=array("Linux","Apache","MySQL","PHP");
usort($data_5,"sortbylen");//通过元素长度排序
print_r($data_5);//Array ( [0] => PHP [1] => MySQL [2] => Linux [3] => Apache )
function sortbylen($one,$two){
if(strlen($one)==strlen($two))
return 0;
else
return (strlen($one)>strlen($two))?1:-1;
}//拆分、合并、分解、接合的数组函数
echo '
';
$data_6=array("Linux","Apache","MySQL","PHP");
print_r(array_slice($data_6,1,2));//取下标为1、2的元素
//Array ( [0] => Apache [1] => MySQL ) 下标重置从0开始
echo '
';print_r(array_slice($data_6,-2,1));//从后边的第二个开始取返回一个,不是从0开始的
//Array ( [0] => MySQL ) 下标重置从0开始
echo '
';print_r(array_slice($data_6,1,2,true));
//Array ( [1] => Apache [2] => MySQL ) 保留原有的下标echo '
';
//array_combine()
$a1=array("OS","WebServer","DataBase","Language");
$a2=array("Linux","Apache","MySQL","PHP");print_r(array_combine($a1,$a2));//第一个参数作为键名,第二个作为值来合并
//Array ( [OS] => Linux [WebServer] => Apache [DataBase] => MySQL [Language] => PHP )echo '
';//array_merge()
$a3=array("OS","WebServer","DataBase","Language");
$a4=array("Linux","Apache","MySQL","PHP");
$a5=$a3+$a4;
print_r($a5);//因为两个数组下标重复所以显示这样
//Array ( [0] => OS [1] => WebServer [2] => DataBase [3] => Language )
echo '
';
print_r(array_merge($a3,$a4));//合并并重新索引
//Array ( [0] => OS [1] => WebServer [2] => DataBase [3] => Language [4] => Linux [5] => Apache [6] => MySQL [7] => PHP )echo '
';//array_intersect()
$a7=array("OS","WebServer","DataBase","Language",1,2,3);
$a8=array("Linux","Apache","MySQL","PHP",2,3,4);
print_r(array_intersect($a7,$a8));//Array ( [5] => 2 [6] => 3 )
echo '
';//array_diff()
$a9=array(1,2,3,4);
$a10=array(3,4,5,6);
print_r(array_diff($a9,$a10));//Array ( [0] => 1 [1] => 2 )
//返回第一个数组跟第二个相差的元素
echo '
';
//使用数组实现堆栈
$b=array(1,2,3,4);
$b[]="a";//入栈
array_push($b,"b","c");//使用函数入栈
print_r($b);//Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => a [5] => b [6] => c )
echo '
';$value=array_pop($b);//使用函数出栈
print_r($b);//Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => a [5] => b )
echo '
';
echo $value;//显示出栈的元素的值 c
echo '
';//使用数组实现队列
$c=array(1,2,3);
print_r($c);//Array ( [0] => 1 [1] => 2 [2] => 3 )
echo '
';
array_unshift($c,"abc","bcd");//入队
print_r($c);//Array ( [0] => abc [1] => bcd [2] => 1 [3] => 2 [4] => 3 )
echo '
';
$values=array_shift($c);//出队
print_r($c);// Array ( [0] => bcd [1] => 1 [2] => 2 [3] => 3 )
echo '
';
unset($c[2]);//删除指定位置元素
print_r($c);//Array ( [0] => bcd [1] => 1 [3] => 3 )
echo '
';
//array_rand() 随机返回数组下标
$arr=array(1,3,4,5,76,7,99,6,2,3);
echo array_rand($arr);//返回的是随机的数组元素的下标
echo $arr[array_rand($arr)];//随机显示数组元素的值
echo '
';
//shuffle() 随机重新排列数组
$arr2=array(32,35,33);
shuffle($arr2);
print_r($arr2);//数组元素位置随机变换
echo '
';
//array_sum() 求和
$arr3=array(1,3,5);
echo array_sum($arr3); //返回9
echo '
';
print_r($arr3);//Array ( [0] => 1 [1] => 3 [2] => 5 )
echo '
';
//range(最小值,最大值,步长)
$arr4=range(0,100,10);
print_r($arr4);//Array ( [0] => 0 [1] => 10 [2] => 20 [3] => 30 [4] => 40 [5] => 50 [6] => 60 [7] => 70 [8] => 80 [9] => 90 [10] => 100 )?>

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics











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,

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

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.

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

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.

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.

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

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.
