PHP:按条件拆分数组

时间:2017-02-20 19:00:09

标签: php arrays checkbox

我有一个数组 $ values $ Tmax = 500

    $values = array (
    array (0 => "MONTENVERS"),
    array (0 => "HUVEAU"),
    array (0 => "PHILOGUI"),
    array (0 => "SEGARD CHRISTINE PATRIMOINE"),
    array (0 => "STIQUEL"),
    array (80 => "LINO"),
    array (86 => "MAUBERT"),
    array (94 => "GOSSELIN" ),
    array (97 => "LIMPA" ),
    array (98 => "CASSIERA" ),
    array (120 => "LES VIGNES"),
    array (123 => "WURTZ"),
    array (124 => "ROUSSET-GAILLARD" ),
    array (124 => "LEGRAND" ),
    array (126 => "SANZ" ),
    array (131 => "VIRONNEAU"),
    array (133 => "SOCCORO"),
    array (136 => "DESNOS"),
    array (178 => "BARRIAC"),
    array (233 => "MONTALBETTI"),
    array (251 => "FOSSET"),
    array (264 => "HERY"),
    array (350 => "HUVEAU"),
    array (352 => "GABSARIC"),
    array (471 => "DSR"),
    array (573 => "RTAIMATE"),
    array (582 => "DEGERMANN"),
    array (589 => "PIERVIE"),
    array (601 => "BLC"),
    array (617 => "SP PATRIMOINE"),
    array (3467 => "RESIDENCE ORPEA DES RIVES D'OR"),
);

我想要2个结果:

  

值和值之和< = $ tmax(nb off迭代可以在3和之间   N)

http://joberjob.com/script/index.php

  

值和值的总和> 5 $ tmax(nb off迭代可以是最多3)

http://joberjob.com/script/index.php

示例:  我有:
Error result

我想要那个
OK result

2 个答案:

答案 0 :(得分:1)

要理解你想要实现的目标是非常困难的 - 即使是从图像中也是如此。但这是你想要的吗?

    $Tmax = 500;

    $collection = [];
    $sum = 0;
    $index = 0;

    foreach ($values as $value) {
        if (!isset($collection[$index])) {
            $collection[$index] = [];
        }

        if (key($value) + $sum < $Tmax) {
            $collection[$index][] = $value;
            $sum += key($value);

            continue;
        }

        if (count($collection[$index]) > 2) {
            $sum = 0;
            $index ++;
        }

        if (!isset($collection[$index])) {
            $collection[$index] = [];
        }

        if (key($value) + $sum < $Tmax) {
            $collection[$index][] = $value;
            $sum += key($value);

            continue;
        }
    }

    print_r($collection);

答案 1 :(得分:0)

$values = array (
array (
        0 => "MONTENVERS" 
),
array (
        0 => "HUVEAU" 
),
array (
        0 => "PHILOGUI" 
),
array (
        0 => "SEGARD CHRISTINE PATRIMOINE" 
),
array (
        0 => "STIQUEL" 
),
array (
        80 => "LINO" 
),
array (
        86 => "MAUBERT" 
),
array (
        94 => "GOSSELIN" 
),
array (
        352 => "GABSARIC" 
),
 array (
        471 => "DSR" 
),
array (
        573 => "RTAIMATE" 
)
);

//$Tmax = 500;

$collection = [];
$sum = 0;
$index = 0;

foreach ($values as $value) {
    if (!isset($collection[$index])) {
        $collection[$index] = [];
    }

    if (key($value) + $sum <= $Tmax) {
        $collection[$index][] = $value;
        $sum += key($value);

        continue;
    }


    if (count($collection[$index]) > 2) {
        $sum = 0;
        $index ++;
    }


    if (!isset($collection[$index])) {
        $collection[$index] = [];
    }

    if (key($value) + $sum > $Tmax) {
        $collection[$index][] = $value;
        $sum += key($value);

        continue;
    }
}

var_dump($collection);die;

?>

此代码显示result

但我想: resultOK