Reputation: 142
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 we sort $Array_Raw
in ascending order then we have a new array like:
array('KEY_5'=>'100','KEY_1'=>'400','KEY_4'=>'450',
'KEY_0'=>'550','KEY_3'=>'600','KEY_2'=>'800');
This can achieved by asort()
function, but I need it with priority
$Array_P
contains priority for keys of $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 priority skip gap.
It could be: $Priority_Gap = 60;
This means $Array_Raw
should be sorted with 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_4
is before KEY_1
is because the value of KEY_1
+ $Priority_Gap > the value of KEY_4
, and KEY_4
has a higher priority than KEY_1
.
However KEY_0
and KEY_3
maintain their relative positions because the value of KEY_0
+ $Priority_Gap
> the value of KEY_3
, but KEY_0
has a higher priority so KEY_0
is always ahead from KEY_3
.
All values of $Array_Raw
are dynamic, $Array_P
is dynamic and $Priority_Gap
is dynamic. Even the size of $Array_Raw
is dynamic. Also, the sorting direction may be set to ascending or descending.
I have been trying to do this for 13 days, only achieved dynamic $Array_Raw
,$Array_P
& $Priority_Gap
, but can't find any way for dynamic size for $Array_Raw
or for dynamic order.
This is my solution but not sorted with Dynamic Size
$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>";
Upvotes: 4
Views: 1708
Reputation: 2945
<?php
$array_raw = [
'key_0' => 550,
'key_1' => 400,
'key_2' => 800,
'key_3' => 600,
'key_4' => 450,
'key_5' => 100,
];
$array_p = [
'key_0' => 4,
'key_1' => 5,
'key_2' => 1,
'key_3' => 6,
'key_4' => 2,
'key_5' => 3,
];
$priority_gap = 60;
asort($array_raw);
/*
the array is already sorted so i just keep values where $a<$b === 1.
and using Boolean algebra and Karnaugh Map i get :
$a<$b $pa<$pb $a+$pg>$b $r
A B C
1 0 0 -1
1 0 1 -1
1 1 0 0
1 1 1 1
return -1 when : AB'C' + AB'C = AB' --> $va < $vb && $pa > $pb
return 1 when : ABC --> $va < $vb && $pa < $pb && $a + $pg > $b
return 0 when : ABC' --> $va < $vb && $pa < $pb && $a + $pg < $b
*/
uksort($array_raw, function($a, $b) use($array_p, $priority_gap, $array_raw) {
$va = $array_raw[$a];
$vb = $array_raw[$b];
$pa = $array_p[$a];
$pb = $array_p[$b];
if($va < $vb && $pa < $pb) {
return -1;
}
if($va < $vb && $pa > $pb && $va + $priority_gap > $vb) {
return 1;
}
return 0;
});
echo "<pre>";
print_r($array_raw);
output :
Array
(
[key_5] => 100
[key_4] => 450
[key_1] => 400
[key_0] => 550
[key_3] => 600
[key_2] => 800
)
Upvotes: 1