【发布时间】:2017-08-06 20:59:57
【问题描述】:
我有一个数组,这个数组包含未排序的值。
例如。
$Array_Raw=array('KEY_0'=>'550','KEY_1'=>'400','KEY_2'=>'800',
'KEY_3'=>'600','KEY_4'=>'450','KEY_5'=>'100');
如果我们按升序对$Array_Raw 进行排序,那么我们有一个新数组,如:
array('KEY_5'=>'100','KEY_1'=>'400','KEY_4'=>'450',
'KEY_0'=>'550','KEY_3'=>'600','KEY_2'=>'800');
这可以通过asort()函数实现,但我需要它优先
$Array_P 包含 $Array_Raw 的键的优先级。
$Array_P=array('KEY_0'=>'4','KEY_1'=>'5','KEY_2'=>'1',
'KEY_3'=>'6','KEY_4'=>'2','KEY_5'=>'3');
$Priority_Gap 是一个包含优先级跳过间隙的变量 (INT)。
可能是:$Priority_Gap = 60;
这意味着$Array_Raw 应按升序排序,并优先获得此预期结果:
$Array_Raw=array('KEY_5'=>'100','KEY_4'=>'450','KEY_1'=>'400',
'KEY_0'=>'550','KEY_3'=>'600','KEY_2'=>'800');
之所以KEY_4在KEY_1之前是因为KEY_1的值+$Priority_Gap>KEY_4的值,而KEY_4的优先级高于KEY_1。
但是KEY_0和KEY_3保持它们的相对位置,因为KEY_0的值+$Priority_Gap>KEY_3的值,但是KEY_0具有更高的优先级,所以KEY_0总是领先于KEY_3.
$Array_Raw 的所有值都是动态的,$Array_P 是动态的,$Priority_Gap 是动态的。甚至$Array_Raw 的大小也是动态的。此外,排序方向可以设置为升序或降序。
我已经尝试了 13 天,只实现了动态 $Array_Raw,$Array_P 和 $Priority_Gap,但找不到任何方法来实现 $Array_Raw 的动态大小或动态订单。
这是我的解决方案,但未按动态大小排序
$array_raw = [
'key_0' => 550,
'key_1' => 400,
'key_2' => 800,
'key_3' => 600
];
$array_p = [
'key_0' => 2,
'key_1' => 3,
'key_2' => 1,
'key_3' => 4
];
$priority_gap = 500;
function Sort_Array_Asc($ARRAY,$PRIORITY,$GAP)
{
if($ARRAY[$PRIORITY[0]] <= $ARRAY[$PRIORITY[1]] + $GAP )
{
if($ARRAY[$PRIORITY[0]] <= $ARRAY[$PRIORITY[2]] + $GAP )
{
if($ARRAY[$PRIORITY[0]] <= $ARRAY[$PRIORITY[3]] + $GAP )
{
$VAL_COMP2 = $PRIORITY[0];
}
else
{
if($ARRAY[$PRIORITY[2]] <= $ARRAY[$PRIORITY[3]] + $GAP )
{
if($ARRAY[$PRIORITY[1]] <= $ARRAY[$PRIORITY[2]] + $GAP )
{
$VAL_COMP2 = $PRIORITY[1];
}
else
{
$VAL_COMP2 = $PRIORITY[2];
}
}
else
{
$VAL_COMP2 = $PRIORITY[3];
}
}
}
else
{
if($ARRAY[$PRIORITY[2]] <= $ARRAY[$PRIORITY[3]] + $GAP )
{
$VAL_COMP2 = $PRIORITY[2];
}
else
{
if($ARRAY[$PRIORITY[2]] <= $ARRAY[$PRIORITY[3]] + $GAP )
{
if($ARRAY[$PRIORITY[1]] <= $ARRAY[$PRIORITY[2]] + $GAP )
{
$VAL_COMP2 = $PRIORITY[1];
}
else
{
$VAL_COMP2 = $PRIORITY[2];
}
}
else
{
$VAL_COMP2 = $PRIORITY[3];
}
}
}
}
else
{
if($ARRAY[$PRIORITY[1]] <= $ARRAY[$PRIORITY[2]] + $GAP )
{
if($ARRAY[$PRIORITY[1]] <= $ARRAY[$PRIORITY[3]] + $GAP )
{
$VAL_COMP2 = $PRIORITY[1];
}
else
{
if($ARRAY[$PRIORITY[2]] <= $ARRAY[$PRIORITY[3]] + $GAP )
{
if($ARRAY[$PRIORITY[1]] <= $ARRAY[$PRIORITY[2]] + $GAP )
{
$VAL_COMP2 = $PRIORITY[1];
}
else
{
$VAL_COMP2 = $PRIORITY[2];
}
}
else
{
$VAL_COMP2 = $PRIORITY[3];
}
}
}
else
{
if($ARRAY[$PRIORITY[2]] <= $ARRAY[$PRIORITY[3]] + $GAP )
{
if($ARRAY[$PRIORITY[1]] <= $ARRAY[$PRIORITY[2]] + $GAP )
{
$VAL_COMP2 = $PRIORITY[1];
}
else
{
$VAL_COMP2 = $PRIORITY[2];
}
}
else
{
if($ARRAY[$PRIORITY[2]] <= $ARRAY[$PRIORITY[3]] + $GAP )
{
if($ARRAY[$PRIORITY[1]] <= $ARRAY[$PRIORITY[2]] + $GAP )
{
$VAL_COMP2 = $PRIORITY[1];
}
else
{
$VAL_COMP2 = $PRIORITY[2];
}
}
else
{
$VAL_COMP2 = $PRIORITY[3];
}
}
}
}
return $VAL_COMP2;
}
function CompareArrays($ARRAY,$PRIORITYX,$GAP)
{
$PRIORITY = array();
asort($PRIORITYX);
$count = 0;
foreach($PRIORITYX as $key => $value)
{
$PRIORITY[$count] = $key;
$count++;
}
$NEWARRAY = $ARRAY;
$C1 = Sort_Array_Asc($NEWARRAY,$PRIORITY,$GAP);
$NEWARRAY[$C1] = max($NEWARRAY) + ($GAP * 2) + 1;
$C2 = Sort_Array_Asc($NEWARRAY,$PRIORITY,$GAP);
$NEWARRAY[$C2] = max($NEWARRAY) + ($GAP * 2) + 1;
$C3 = Sort_Array_Asc($NEWARRAY,$PRIORITY,$GAP);
$NEWARRAY[$C3] = max($NEWARRAY) + ($GAP * 2) + 1;
$C4 = Sort_Array_Asc($NEWARRAY,$PRIORITY,$GAP);
return array($C1,$C2,$C3,$C4);
}
$SortedArray = CompareArrays($array_raw,$array_p,$priority_gap);
echo "<br><br><br><br><pre>";
print_r($SortedArray);
echo "</pre><br><br><br><br>";
【问题讨论】:
-
你的问题中的所有点在做什么?请为此目的使用按钮格式化您的代码:
{ }。 -
为什么不将这些数组组合成一个对象数组:
new Priority($value, $priority, $priorityGap)。然后您可以使用 usort 在一个自定义排序函数中检查所有这些条件。 -
另一种对优先级数组进行排序的解决方案,然后按正确的顺序获取值。
-
问题可能很好,但无法理解,因为很多文字和逻辑没有正确解释。您能否发布实际输入和您想要的所需输出(例如真正的数组而不是这种文本格式)。可能只有某个人可以帮助你。根据实际输入值解释逻辑(它们需要是正确的数组而不是文本表示)
-
...并展示您的编码尝试