In the PHP tutorial, array is a powerful data type. It can do many things. It can store different data types in an array. Below we list the common operations of arrays, sorting, and sorting arrays by key name. etc.
/* Common functions for arrays
*
* Array sorting function
* sort()
* rsort()
* usort()
* asort()
* arsort()
* uasort()
* ksort()
* krsort()
* uksort()
* uatsort()
* natcasesort()
* array_multisort()
*
* 1. Simple array sorting
* sort() rsort()
* 2. Sort the array according to the key name
* ksort() krsort()
* 3. Sort the array according to the value of the element
* asort() arsort()
* 4. Sort the array according to the "natural number sorting" method
* natsort()//Comparison of uppercase and lowercase letters natcasescort()//Comparison of insensitive letters
* 5. Sort the array according to user-defined rules
* usort() uasort() uksort() sorts keys
* 6. Sorting of dimensional arrays
* array_multisort()
*
* Array functions for splitting, merging, decomposing, and joining
* 1.array_slice()
* 2.array_splice()//Delete
* 3.array_combine()//Combine
* 4.array_merge();//Merge
* 5.array_intersect();//Intersection of multiple arrays
* 6.array_diff();//Return the difference set of multiple arrays
*
* Array and data structure functions
* 1. Use arrays to implement stack //First in, last out
* array_push() array_pop()
* 2. Use arrays to implement queues //first in, first out
* array_unshift() array_shift() unset()
*
*
* Other functions related to array operations
* array_rand()
* shuffle()
* array_sum()
* range()
*///The use of simple array sorting
$data=array(5,8,1,7,2);
sort($data);//Sort elements from small to large
print_r($data);//Array ( [0] => 1 [1] => 2 [2] => 5 [3] => 7 [4] => 8 )
rsort($data);//Sort elements from large to small
print_r($data);//Array ( [0] => 8 [1] => 7 [2] => 5 [3] => 2 [4] => 1 )//Example of sorting based on key name
$data_2=array(5=>"five",8=>"eight",1=>"one",7=>"seven",2=>"two");
ksort($data_2);//Sort the subscripts of the array from small to large
print_r($data_2);//Array ( [1] => one [2] => two [5] => five [7] => seven [8] => eight )
krsort($data_2);//Sort the subscripts of the array from large to small
print_r($data_2);//Array ( [8] => eight [7] => seven [5] => five [2] => two [1] => one )
//Sort the array according to the value of the element
$data_3=array("1"=>"Linux","a"=>"Apache","m"=>"MySQL","l"=>"PHP");
//asort() arsort The difference between sort() rsort() is that the former keeps the original key names after sorting, while the latter does not keep the original key names, and the key names start from 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 )//Sort the array according to the "natural number sorting method" (the shorter one from 0-9 is given priority)
$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 '
';//User-defined sorting function
echo '
';
$data_5=array("Linux","Apache","MySQL","PHP");
usort($data_5,"sortbylen");//Sort by element length
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;
}//Array functions for splitting, merging, decomposing, and joining
echo '
';
$data_6=array("Linux","Apache","MySQL","PHP");
print_r(array_slice($data_6,1,2));//Remove the elements marked 1 and 2
//Array ( [0] => Apache [1] => MySQL ) subscript reset starts from 0
echo '
';print_r(array_slice($data_6,-2,1));//Retrieve one starting from the second one at the end, not starting from 0
//Array ([0] => MySQL) subscript reset starts from 0
echo '
';print_r(array_slice($data_6,1,2,true));
//Array ( [1] => Apache [2] => MySQL ) retain the original subscriptecho '
';
//array_combine()
$a1=array("OS","WebServer","DataBase","Language");
$a2=array("Linux","Apache","MySQL","PHP");print_r(array_combine($a1,$a2));//The first parameter is used as the key name, and the second parameter is used as the value to merge
//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);//Because the two array subscripts are repeated, it is displayed like this
//Array ( [0] => OS [1] => WebServer [2] => DataBase [3] => Language )
echo '
';
print_r(array_merge($a3,$a4));//Merge and re-index
//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 )
//Return the difference between the first array and the second element
echo '
';
//Use array to implement stack
$b=array(1,2,3,4);
$b[]="a";//Push into stack
array_push($b,"b","c");//Use function to push onto the stack
print_r($b);//Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => a [5] => ; b [6] => c )
echo '
';$value=array_pop($b);//Use function to pop
print_r($b);//Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => a [5] => ; b )
echo '
';
echo $value;//Display the value of the element popped off the stack c
echo '
';//Use array to implement queue
$c=array(1,2,3);
print_r($c);//Array ( [0] => 1 [1] => 2 [2] => 3 )
echo '
';
array_unshift($c,"abc","bcd");//Enqueue
print_r($c);//Array ( [0] => abc [1] => bcd [2] => 1 [3] => 2 [4] => 3 )
echo '
';
$values=array_shift($c);//Dequeue
print_r($c);// Array ( [0] => bcd [1] => 1 [2] => 2 [3] => 3 )
echo '
';
unset($c[2]);//Delete the element at the specified position
print_r($c);//Array ( [0] => bcd [1] => 1 [3] => 3 )
echo '
';
//array_rand() Randomly returns array subscript
$arr=array(1,3,4,5,76,7,99,6,2,3);
echo array_rand($arr);//Returns the subscript of a random array element
echo $arr[array_rand($arr)];//Randomly display the value of the array element
echo '
';
//shuffle() randomly rearranges the array
$arr2=array(32,35,33);
shuffle($arr2);
print_r($arr2);//Array element position is randomly transformed
echo '
';
//array_sum() Sum
$arr3=array(1,3,5);
echo array_sum($arr3); //Return 9
echo '
';
print_r($arr3);//Array ( [0] => 1 [1] => 3 [2] => 5 )
echo '
';
//range(minimum value, maximum value, step size)
$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 )?>

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


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

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

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.

SublimeText3 Chinese version
Chinese version, very easy to use

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version
SublimeText3 Linux latest version