繁体   English   中英

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

[英]Compare two array of values and create one array 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
        )

)

实际输出将类似于,箭头显示添加数据的流程。 循环将以相同的方式工作。

在此处输入图片说明

我尝试过但不矫正

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

这是我的解决方案:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM