I have an array, this array contains unsorted values.
Ex.
$Array_Raw=array('KEY_0'=>'550','KEY_1'=>'400','KEY_2'=>'800',
'KEY_3'=>'600','KEY_4'=>'450','KEY_5'=>'100');
If you sort $Array_Rawin ascending order, then we have a new array:
array('KEY_5'=>'100','KEY_1'=>'400','KEY_4'=>'450',
'KEY_0'=>'550','KEY_3'=>'600','KEY_2'=>'800');
This can be achieved using a function asort(), but I need it with priority
$Array_Pcontains priority for keys $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 is a variable (INT) that contains a space for priority skips.
It could be: $Priority_Gap = 60;
This means that you $Array_Rawshould sort in ascending order with priority for this expected result:
$Array_Raw=array('KEY_5'=>'100','KEY_4'=>'450','KEY_1'=>'400',
'KEY_0'=>'550','KEY_3'=>'600','KEY_2'=>'800');
The reason KEY_4before KEY_1is because the value KEY_1+ $ Priority_Gap> value KEY_4and KEY_4has a higher priority than 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>";