2016-07-14 170 views
0

我正在开发一个应用程序devide一组值customers.Each客户有个限度值(根据分配给客户的百分比总价值分割)比较两个数组值,并创建一个PHP数组

例如: 总价值为339269 客户1分配了39%的百分比 - 然后将价值限制变为:132314 客户2分配了34%的百分比 - 然后将价值限制变为:115351 客户3分配了27%的百分比 - 限制成为:91602

$valueArray = array('67212','37256','32909','29847','28529','27643','25356','25274','23604','23058','18581'); 
$customer = array('132314','115351','91602'); 

我想通过将每个值添加到列而不超过其限制来排列客户列中的值数组。

停止放会像

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 
     ) 

) 

实际产出会像,示出添加数据的流动的箭头的阵列。循环将以相同的方式工作。

enter image description here

,我试图像,但没有得到当期的

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)); 
+0

问题在哪里?您应该编写一些代码,并将问题确切地显示在哪里。 – Mimouni

+0

请检查我添加的 –

+0

对不起,但我无法理解你在这里要做什么。百分比在哪里,在你的例子中$ customer的数字是多少?为什么在你的输出示例中有一个空的[1] [4]。请让你的问题更清楚。 – Michel

回答

0

这是我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; 
} 

你也有错误的预期输出 - 第一个客户是在其限制

+0

是...我希望将这样的值放在我们分配最大百分比的列中,我是指具有最大限制的列限制。 –

+0

@alialwafaa编辑...下次尝试更准确地描述您的代码中您的预期输出是 – StormRideR

+0

我们如何将这些值添加到具有最大限制的列中。 –