比较两个值数组并创建一个PHP数组

时间:2016-07-14 11:08:17

标签: php loops

我正在开发一个应用程序,为客户分配一组价值。每个客户都有一个限额值(根据分配给客户的百分比拆分总价值)

例如: 总价值为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');

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

Out put将是一个类似

的数组
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));

1 个答案:

答案 0 :(得分:0)

这是我的解决方案:

        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;
}

您的预期输出也有错误 - 第一位客户已超出其限制

相关问题