2016-07-14 203 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

我們如何將這些值添加到具有最大限制的列中。 –