


Two-dimensional array sorting also provides a function array_multisort in PHP that can be sorted directly. Below I will introduce that in addition to using array_multisort to sort arrays, we have also written some custom two-dimensional array sorting methods.
Sometimes, in order to achieve a certain purpose, it is necessary to sort a two-dimensional array. Now I will share how to achieve it.
The code is as follows | Copy code | ||||
'1' => array ( 'date' => '2011-08-18', 'num' => 5 ) , '2' => array ( 'date' => '2011-08-20', 'num' => 3 ) , '3' => array ( 'date' => '2011-08-17', 'num' => 10 ) ) ; $result = sysSortArray($arr,'num'); The effect after running like this is: $arr=array ( '1' => array ( 'date' => '2011-08-18', 'num' => 3 ) , '2' => array ( 'date' => '2011-08-20', 'num' => 5 ) , '3' => array ( 'date' => '2011-08-17', 'num' => 10 ) ) ; Functions used: /** * @package 二维数组排序 * @version $Id: FunctionsMain.inc.php,v 1.32 2011/09/24 11:38:37 wwccss Exp $ * * * Sort an two-dimension array by some level two items use array_multisort() function. * * sysSortArray($Array,"Key1","SORT_ASC","SORT_RETULAR","Key2";……) * @author lamp100 * @param array $ArrayData the array to sort. * @param string $KeyName1 the first item to sort by. * @param string $SortOrder1 the order to sort by("SORT_ASC"|"SORT_DESC") * @param string $SortType1 the sort type("SORT_REGULAR"|"SORT_NUMERIC"|"SORT_STRING") * @return array sorted array. */ function sysSortArray($ArrayData,$KeyName1,$SortOrder1 = "SORT_ASC",$SortType1 = "SORT_REGULAR") { If(!is_array($ArrayData)) { return $ArrayData; } // Get args number. $ArgCount = func_num_args(); // Get keys to sort by and put them to SortRule array. for($I = 1;$I { $Arg = func_get_arg($I); If(!eregi("SORT",$Arg)) { $KeyNameList[] = $Arg; $SortRule[] = '$'.$Arg; } else { $SortRule[] } } // Get the values according to the keys and put them to array. foreach($ArrayData AS $Key => $Info) { foreach($KeyNameList AS $KeyName) { ${$KeyName}[$Key] = $Info[$KeyName]; } } // Create the eval string and eval it. $EvalString = 'array_multisort('.join(",",$SortRule).',$ArrayData);'; eval ($EvalString); Return $ArrayData; } |
In addition: the array_multisort function is also very powerful. For details, please refer to the PHP manual, which is very detailed.
We can use the array_multisort() function. The array_multisort() function sorts multiple arrays or multidimensional arrays.
The array in the parameter is treated as a table column and sorted by row - this is similar to the functionality of SQL's ORDER BY clause. The first array is the main array to be sorted. If the rows (values) in the array compare to be the same, they will be sorted according to the size of the corresponding value in the next input array, and so on.
The first parameter is an array, and each subsequent parameter may be an array or one of the following sort order flags (the sort flag is used to change the default sort order):
•SORT_ASC - Default, sort in ascending order. (A-Z)
•SORT_DESC - Sort in descending order. (Z-A)
You can then specify the sorting type:
•SORT_REGULAR - Default. Arrange each item in regular order.
•SORT_NUMERIC - Sort each item in numerical order.
•SORT_STRING - Sort each item in alphabetical order.
Syntax: array_multisort(array1,sorting order,sorting type,array2,array3...)
•array1: required. Specifies the input array.
•Sort order: optional. Specify the order of sorting. Possible values are SORT_ASC and SORT_DESC.
•sorting type: optional. Specifies the sorting type. Possible values are SORT_REGULAR, SORT_NUMERIC, and SORT_STRING.
•array2: optional. Specifies the input array.
•array3: optional. Specifies the input array.
String key names will be preserved, but numeric keys will be re-indexed, starting at 0 and increasing by 1. The sort order and sort type can be set after each array. If not set, each array parameter will use its default value.
Here is an example:
The code is as follows | Copy code | ||||
'; for($i=0; $i { $arr[$i]['val'] = mt_rand(1, 100); $arr[$i]['num'] = mt_rand(1, 100); } echo ' '; print_r($arr); echo ''; echo 'Extract the key val from the two-dimensional array and separate it into another array:'.' '; foreach ($arr as $key => $row) { $vals[$key] = $row['val']; $nums[$key] = $row['num']; } echo ' '; print_r($vals); echo ''; echo 'Sort it:'.' '; array_multisort($vals, SORT_ASC, $arr); echo ' '; print_r($vals); echo ''; ?> |
Run result:
The two-dimensional array is as follows:
Array
(
[0] => Array
(
[val] =>
[num] => 49
)
(
[val] =>
[num] =>
)
[2] => Array
(
)
[3] => Array
(
[val] =>
[num] =>
)
[4] => Array
(
[val] => 19
[num] =>
)
(
[val] =>
[num] =>
)
)
Extract the key val from the two-dimensional array and separate it into another array:
(
[0] => 46
[1] => 8
[2] => 37
[3] => 32
[5] => 30
)
Sort it:
Array
(
[0] => 8
[1] => 19
[2] => 30
[3] => 32
[4] => 37
[5] => 46
)
We will get a two-dimensional array sorted by val in ascending order.

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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
