使用自定义功能在array_multisort() [英] Use array_multisort() with custom function
本文介绍了使用自定义功能在array_multisort()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下的数组,我想它根据另一个数组,而不是进行排序 DESC
或 ASC
$阵列=阵列(
'注意'=>阵列(测试,test1的','TEST2','TEST3','TEST4'),
今年'=>阵列('2011','2010','2012','2009','2010'),
'型'=>阵列(日记,会议,会议,会议,会议),
);
是否有可能有一个自定义函数做一个在array_multisort()内?
例如:
在array_multisort($数组['类型'],$数组['年'],custom_function,$阵列['注']);
而不是:
在array_multisort($数组['类型'],$数组['年'],SORT_ASC,$阵列['注']);
解决方案
如果你知道数组的深度可以简单地套用的 usort 上进行排序每个数组元素。
下面是根据哪些订单为例,自定义的数组:
< PHP
为了$ =阵列(
'第一',
'第二',
'第三',
'第四',
'第五'
);$阵列=阵列(
阵列(
'第二',
'第四',
'第一',
'第三'
)
阵列(
'第二',
'第一'
)
);的foreach($数组和放大器; $值){
usort($值,函数($ A,$ B)使用($顺序){
返回array_search($ A,$顺序)> array_search($ B,$顺序);
});
}
未设置($值);后续代码var_dump($数组);
/ *
阵列(2){
[0] =>
阵列(4){
[0] =>
串(5)第一次
[1] =>
串(6)第二
[2] =>
串(5)第三
[3] =>
字符串(6)第四
}
[1] =>
阵列(2){
[0] =>
串(5)第一次
[1] =>
串(6)第二
}
}
* /
如果你不知道数组可以有多深,这使我心中唯一的解决办法是递归函数:
< PHP
为了$ =阵列(
'第一',
'第二',
'第三',
'第四',
'第五'
);$阵列=阵列(
阵列(
'第二',
'第四',
'第一',
'第三'
)
阵列(
阵列('第二','第一'),
阵列('四','三')
)
);功能custom_multisort($数组$顺序){
的foreach($数组和放大器; $值){
如果(is_array($值[0])){
$值= custom_multisort(价值$,$顺序);
}其他{
usort($值,函数($ A,$ B)使用($顺序){
返回array_search($ A,$顺序)> array_search($ B,$顺序);
});
}
}
返回$阵列;
}$阵列= custom_multisort($数组$顺序);后续代码var_dump($数组);
/ *
阵列(2){
[0] =>
阵列(4){
[0] =>
串(5)第一次
[1] =>
串(6)第二
[2] =>
串(5)第三
[3] =>
字符串(6)第四
}
[1] =>
阵列(2){
[0] =>
阵列(2){
[0] =>
串(5)第一次
[1] =>
串(6)第二
}
[1] =>
阵列(2){
[0] =>
串(5)第三
[1] =>
字符串(6)第四
}
}
}
* /
I have the following array and I would like to sort it according another array and not DESC
or ASC
$array = array(
'note' => array('test', 'test1', 'test2', 'test3', 'test4'),
'year' => array('2011','2010', '2012', '2009', '2010'),
'type' => array('journal', 'conference', 'conference', 'conference','conference'),
);
Is it possible to do it with a custom function inside an array_multisort()?
For example:
array_multisort($array['type'], $array['year'], custom_function, $array['note']);
instead of:
array_multisort($array['type'], $array['year'], SORT_ASC, $array['note']);
解决方案
If you know the depth of the array you can simply apply usort on each array element to be sorted.
Here is an example which orders according to a custom array:
<?php
$order = array(
'first',
'second',
'third',
'fourth',
'fifth'
);
$array = array(
array(
'second',
'fourth',
'first',
'third'
),
array(
'second',
'first'
)
);
foreach($array as &$value) {
usort($value, function($a, $b) use($order) {
return array_search($a, $order) > array_search($b, $order);
});
}
unset($value);
var_dump($array);
/*
array(2) {
[0]=>
array(4) {
[0]=>
string(5) "first"
[1]=>
string(6) "second"
[2]=>
string(5) "third"
[3]=>
string(6) "fourth"
}
[1]=>
array(2) {
[0]=>
string(5) "first"
[1]=>
string(6) "second"
}
}
*/
If you don't know how deep the array can go, the only solution that comes to my mind is a recursive function:
<?php
$order = array(
'first',
'second',
'third',
'fourth',
'fifth'
);
$array = array(
array(
'second',
'fourth',
'first',
'third'
),
array(
array('second', 'first'),
array('fourth', 'third')
)
);
function custom_multisort($array, $order) {
foreach($array as &$value) {
if(is_array($value[0])) {
$value = custom_multisort($value, $order);
} else {
usort($value, function($a, $b) use($order) {
return array_search($a, $order) > array_search($b, $order);
});
}
}
return $array;
}
$array = custom_multisort($array, $order);
var_dump($array);
/*
array(2) {
[0]=>
array(4) {
[0]=>
string(5) "first"
[1]=>
string(6) "second"
[2]=>
string(5) "third"
[3]=>
string(6) "fourth"
}
[1]=>
array(2) {
[0]=>
array(2) {
[0]=>
string(5) "first"
[1]=>
string(6) "second"
}
[1]=>
array(2) {
[0]=>
string(5) "third"
[1]=>
string(6) "fourth"
}
}
}
*/
这篇关于使用自定义功能在array_multisort()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文