i am developing an application to devide a set of values to customers.Each customer have a limit value (Total value split based on percentage assigned to customer)
For example: Total value is 339269 Customer 1 assigned a percentage of 39% - then value limit become : 132314 Customer 2 assigned a percentage of 34% - then value limit become : 115351 Customer 3 assigned a percentage of 27% - then value limit become : 91602
$valueArray = array('67212','37256','32909','29847','28529','27643','25356','25274','23604','23058','18581');
$customer = array('132314','115351','91602');
I want to arrange the value array in customer column by adding each value to column without crossing its limit.
Out put will be an array like
Array
(
[0] => Array
(
[0] => 67212
[1] => 27643
[2] => 25356
[3] => 18581
)
[1] => Array
(
[0] => 37256
[1] => 28529
[2] => 25274
[3] => 23058
)
[2] => Array
(
[0] => 32909
[1] => 29847
[2] => 23604
)
)
Real Output will be like, the arrow showing the flow of adding data. loop will work same way.
And i tried like, but not getting currect
function parse(array $valueArr, array $customerArr)
{
$customerCount = count($customerArr);
$chunkedValueArr = array_chunk($valueArr, $customerCount);
$temp = array_fill(0, $customerCount, array());
$i = 0;
foreach ($chunkedValueArr as $item) {
foreach ($item as $key => $value) {
//echo $customerArr[$key]."<br/>";
$pre_existing = isset($temp[$key]) ? array_sum($temp[$key]) : 0;
if($pre_existing+$value <= $customerArr[$key]){
$temp[$key][] = $value;
}
}
$temp = rotateArray($temp);
$customerArr = rotateArray($customerArr);
$i++;
}
// if $i is odd
if ($i & 1) {
$temp = rotateArray($temp);
//$customerArr = rotateArray($customerArr);
}
return $temp;
}
function rotateArray(array $arr)
{
$rotatedArr = array();
//set the pointer to the last element and add it to the second array
array_push($rotatedArr, end($arr));
//while we have items, get the previous item and add it to the second array
for($i=0; $i<sizeof($arr)-1; $i++){
array_push($rotatedArr, prev($arr));
}
return $rotatedArr;
}
echo "<pre>";
print_r(parse($valueArray, $customer));
This is my sollution:
function parse(array $valueArr, array $customerArr)
{
$customerCount =count($customerArr);
$temp =array_fill(0, $customerCount, array());
$customersTotal=array_fill(0, $customerCount, 0);
$maxID= array_keys($customerArr, max($customerArr))[0];
$i =0;
$step=1;
foreach($valueArr as $item)
{
$done =false;
$attempts=0;
while(!$done)
{
//switching directions of traversing
if($i < 0)
{
$i =0;
$step=1;
}
elseif($i >= $customerCount)
{
$i =$customerCount - 1;
$step=-1;
}
if($customersTotal[$i] + $item <= $customerArr[$i])
{
$temp[$i][]=$item;
$customersTotal[$i]+=$item;
$done =true;
}
else{
$attempts++;
if($attempts>$customerCount*2)
{
$i=$maxID;
$temp[$i][]=$item;
$customersTotal[$i]+=$item;
$done =true;
}
}
$i+=$step;
}
}
return $temp;
}
You also have error in your expected output - first customer is over its limt
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.