如何用PHP对数组排序
假设我有这样一个数组:如何用PHP对数组排序,php,arrays,sorting,Php,Arrays,Sorting,假设我有这样一个数组: $arr_sequences = array('00-02', '02-03', '03-07', '23-03', '03-23', '07-11', '11-10', '11-11'); 如何对数组进行排序,使值如下所示: $arr_sequences = array('00-02', '02-03', '03-23', '23-03', '03-07', '07-11', '11-11', '11-10'); 如果我们仔细观察,每个值都有一个
$arr_sequences =
array('00-02', '02-03', '03-07', '23-03', '03-23', '07-11', '11-10', '11-11');
如何对数组进行排序,使值如下所示:
$arr_sequences =
array('00-02', '02-03', '03-23', '23-03', '03-07', '07-11', '11-11', '11-10');
如果我们仔细观察,每个值都有一个id(代码),它被-
例如:
$arr_sequences[2] = '03-07'; // '07' as last code, then search another value with '07' in front of the value
那么下一个索引值应该是
$arr_sequences[5] = '07-11'; // '07' front, then search '11' as next value
目标是在不损失任何长度的情况下对数组进行排序
我尝试过树形算法,但我无法让它工作。疯狂的方法:
$arr_sequences = array('00-02', '02-03', '03-07', '23-03', '03-23', '07-11', '11-10', '11-11'); // Input
$permutations = permutations($arr_sequences); // Generating permutations
// Generating a regex
$n = count($arr_sequences);
$regex = '\d+';
for($i=1;$i<$n;$i++){
$regex .= '-(\d+)-\\'.$i;
}
$regex .= '-\d+';
$sorted = preg_grep('#'.$regex.'#', $permutations); // Filtering the permutations
sort($sorted); // re-index the keys
//generating the desired output
$output = array();
foreach($sorted as $key => $sample){
$temp = explode('-', $sample);
$c = count($temp); // Micro-optimization, yeaaaah!
for($i=0;$i<$c;$i+=2){
$output[$key][] = $temp[$i] . '-' . $temp[$i+1];
}
}
print_r($output); // printing
// Function from http://stackoverflow.com/a/14998162
function permutations($elements) {
if(count($elements)<2) return $elements;
$newperms= array();
foreach($elements as $key=>$element) {
$newelements= $elements;
unset($newelements[$key]);
$perms= permutations($newelements);
foreach($perms as $perm) {
$newperms[]= $element."-".$perm;
}
}
return $newperms;
}
似乎只有一种解决方案:在php中使用蛮力方式。(未测试):
这是什么排序顺序?你能把你试过的贴出来吗?我不明白这里元素之间的关系。你能解释一下吗?如果你能解释一下排序,那么是的。你可以很容易地用javascript为数组创建自定义排序。所以,有点像多米诺骨牌?这不是排序练习,而是计算链接的问题。问题是复杂的,即使在给出的示例中,至少存在三种可能的解决方案,但在某些情况下,并非所有元素都被使用。整套设备是否保证完整完成?我们应该如何对链接进行优先级排序(如果可以进行优先级排序),或者算法应该迭代所有可能的解决方案以找到一个完整的解决方案?哇,谢谢hamza,谢谢你们所有人的努力,但hamza首先回答了。我已经试了3天了,没有解决办法_TT@KoujiAkiraZettai makeruna,gambatte~:)不起作用<代码>[2]=>03-07[3]=>03-23[4]=>07-11([3]不正确)。是的,但仍然。。谢谢你的努力stefan_ubHanksKouji。。是的,你是对的,FDL。希望以后能解决这个问题。。。
Array
(
[0] => Array
(
[0] => 00-02
[1] => 02-03
[2] => 03-23
[3] => 23-03
[4] => 03-07
[5] => 07-11
[6] => 11-11
[7] => 11-10
)
)
<?php
$arr = array('00-02', '02-03', '03-07', '23-03', '03-23', '07-11', '11-10', '11-11');
function findMatches($i, $val_ignore, $arr) {
$arr_matches = array();
foreach($arr as $key => $val) {
$j = substr_replace($val, '', 2);
if (($i == $j) && ($val != $val_ignore))
$arr_matches[] = $val;
}
return $arr_matches;
}
$arr_sorted = array();
foreach($arr as $key => $val) {
$i = substr_replace($val, '', 0, 3);
if (!in_array($val, $arr_sorted)) $arr_sorted[] = $val;
$arr_sorted = array_merge($arr_sorted, findMatches($i, $val, $arr));
}
?>