php关于array
复制代码 代码如下:
$arr1 = array(1,9,5);
$arr2 = array(6,2,4);
array_multisort($arr1,$arr2);
print_r($arr1); // 得到的顺序是1,5,9
print_r($arr2); // 得到的顺序是6,4,2
我估计两个数组的值自始至终都是对应着的:1对应6,9对应2,5对应4。
我们再加多一个数组看看会怎样:
复制代码 代码如下:
$arr1 = array(1,9,5);
$arr2 = array(6,2,4);
$arr3 = array(3,7,8);
array_multisort($arr1,$arr2,$arr3);
查看结果,1自始至终都对应6对应3,其它项也是如此。这种对应关系就是手册中所谓的“排序时保留原有的键名关联”。
另外也可以把每个数组想像成数据库表的一列。而对应着的1,6,3为一数据行,9,2,7为另一数据行。。。
array_multisort会先按第一个数组(想像成列)排序,如果第一个数组(列)的值相同,则按第二个数组(列)排序。
具体可以用下面的程式来测试:
复制代码 代码如下:
$arr1 = array(1,9,5,9);
$arr2 = array(6,2,4,1);
$arr3 = array(3,7,8,0);
array_multisort($arr1,$arr2,$arr3);
可以想像这里$arr3的结果是(3,8,0,7)。
二、接下来讲解array_multisort的参数。这个函数的参数很灵活。最简单的情况是如上面所示的以1个或n个数组作为参数,需要注意的是每个数组的项数要一样,否则会warning导致排序失效。
像这样array_multisort($arr1,$arr2,$arr3); 默认是所有数组都是升序排列,如果想对$arr2降序,并当作字符串去比较,就要写成:
array_multisort($arr1, $arr2, SORT_DESC, SORT_STRING, $arr3);
每个array后面可以跟一个排序顺序标志或一个排序类型标志,或者两种标志同时出现。但是每种排序标志在每个数组后面只能出现一个。
详细如下:
排序顺序标志:
SORT_ASC - 按照上升顺序排序(默认)
SORT_DESC - 按照下降顺序排序
排序类型标志:
SORT_REGULAR - 将项目按照通常方法比较(默认)
SORT_NUMERIC - 将项目按照数值比较
SORT_STRING - 将项目按照字符串比较
三、最后是array_multisort有什么实际作用。
我们通常有一些多维数组需要排序:
$guys = Array
(
[0] => Array
(
[name] => jake
[score] => 80
[grade] => A
)
[1] => Array
(
[name] => jin
[score] => 70
[grade] => A
)
[2] => Array
(
[name] => john
[score] => 80
[grade] => A
)
[3] => Array
(
[name] => ben
[score] => 20
[grade] => B
)
)
例如我们想按成绩倒序排列,如果成绩相同就按名字的升序排列。这时我们就需要根据$guys的顺序多弄两个数组出来:$scores = array(80,70,80,20);$names = array('jake','jin','john','ben');然后array_multisort($scores, SORT_DESC, $names, $guys);就行了还能不能更灵活一点呢,每次想排序都要另外弄些数组出来吗?其实在qeephp的helper_array类里面已经封装得很好,下面是它的两个方法,需要的人自己修改一下就可以用了:
复制代码 代码如下:
/**
* 根据指定的键对数组排序
*
* 用法:
* @code php
* $rows = array(
* array('id' => 1, 'value' => '1-1', 'parent' => 1),
* array('id' => 2, 'value' => '2-1', 'parent' => 1),
* array('id' => 3, 'value' => '3-1', 'parent' => 1),
* array('id' => 4, 'value' => '4-1', 'parent' => 2),
* array('id' => 5, 'value' => '5-1', 'parent' => 2),
* array('id' => 6, 'value' => '6-1', 'parent' => 3),
* );
*
* $rows = Helper_Array::sortByCol($rows, 'id', SORT_DESC);
* dump($rows);
* // 输出结果为:
* // array(
* // array('id' => 6, 'value' => '6-1', 'parent' => 3),
* // array('id' => 5, 'value' => '5-1', 'parent' => 2),
* // array('id' => 4, 'value' => '4-1', 'parent' => 2),
* // array('id' => 3, 'value' => '3-1', 'parent' => 1),
* // array('id' => 2, 'value' => '2-1', 'parent' => 1),
* // array('id' => 1, 'value' => '1-1', 'parent' => 1),
* // )
* @endcode
*
* @param array $array 要排序的数组
* @param string $keyname 排序的键
* @param int $dir 排序方向
*
* @return array 排序后的数组
*/
static function sortByCol($array, $keyname, $dir = SORT_ASC)
{
return self::sortByMultiCols($array, array($keyname => $dir));
} /**
* 将一个二维数组按照多个列进行排序,类似 SQL 语句中的 ORDER BY
*
* 用法:
* @code php
* $rows = Helper_Array::sortByMultiCols($rows, array(
* 'parent' => SORT_ASC,
* 'name' => SORT_DESC,
* ));
* @endcode
*
* @param array $rowset 要排序的数组
* @param array $args 排序的键
*
* @return array 排序后的数组
*/
static function sortByMultiCols($rowset, $args)
{
$sortArray = array();
$sortRule = '';
foreach ($args as $sortField => $sortDir)
{
foreach ($rowset as $offset => $row)
{
$sortArray[$sortField][$offset] = $row[$sortField];
}
$sortRule .= '$sortArray[\'' . $sortField . '\'], ' . $sortDir . ', ';
}
if (empty($sortArray) empty($sortRule)) { return $rowset; }
eval('array_multisort(' . $sortRule . '$rowset);');
return $rowset;
}

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



Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

When programming in PHP, we often need to merge arrays. PHP provides the array_merge() function to complete array merging, but when the same key exists in the array, this function will overwrite the original value. In order to solve this problem, PHP also provides an array_merge_recursive() function in the language, which can merge arrays and retain the values of the same keys, making the program design more flexible. array_merge

Inobject-orientedprogramming,inheritanceallowsustocreatenewclassesthatinheritthepropertiesandmethodsofanexistingclass.Thispowerfulconceptenablescodereuse,modularity,andextensibilityinourprograms.Beforedivingintoaccessingparentclassattributes,let'shav

In PHP, there are many powerful array functions that can make array operations more convenient and faster. When we need to combine two arrays into an associative array, we can use PHP's array_combine function to achieve this operation. This function is actually used to combine the keys of one array as the values of another array into a new associative array. Next, we will explain how to use the array_combine function in PHP to combine two arrays into an associative array. Learn about array_comb
