Home>
$setPositionPercentage = ['upper' =>21, 'middle' =>67, 'low' =>14];
        if (array_sum ($setPositionPercentage)>100) {
            foreach ($setPositionPercentage as $key =>$val) {
                for ($i = 100;$i&array;sum_ $setPositionPercentage);$i ++) {
                    $setPositionPercentage [$key] + = -1;
                }
            }
        }
        var_dump ($setPositionPercentage);
        / *
          array: 3 [▼
            "upper" =>20.0
            "middle" =>66.0
            "low" =>14.0
          ]
       */

Why is "-1" for the key of"upper","middle"from the above vardump result?
In the above loop and foreach nesting logic, -1 was incremented only for the upper key value, and when it reached 100, it was interpreted that it would exit the loop. Therefore, in the for loop, $key is I'm not sure if"upper"will become"middle" ;.
The following values ​​were assumed as return values.
/ *
array: 3 [▼
"upper"=>19.0
"middle"=>67.0
"low"=>14.0
]
*/

php
  • Answer # 1

    You won't meet the for statement in the second week

    foreach ($setPositionPercentage as $key =>$val) {
        // (Week 1) $i = 100 array_sum ($setPositionPercentage) = 102 (Week 2) $i = 101 array_sum ($setPositionPercentage) = 101
        for ($i = 100;$i&array;sum_ $setPositionPercentage);$i ++) {
          $setPositionPercentage [$key] + = -1;// (Week 1) array_sum ($setPositionPercentage) = 101 (Week 2)
        }
    }

  • Answer # 2

      

    In the foreach nesting logic, -1 is incremented only for the upper key value, and when it reaches 100, the loop is exited.

    The interpretation of

    is incorrect.

    for ($i = 100;$i&array;sum_ $setPositionPercentage);$i ++) {
     // Contents omitted
    }


    Start $i from 100 and process while $i is smaller than array_sum ($setPositionPercentage)

    It is written only as

    .

    How to reduce array_sum ($setPositionPercentage),
    If you compare how the for statement loops, you should be convinced that the current result is ...


    Reuse the current code

      

    It is incremented by -1 only for the upper key value, and when it reaches 100, it exits the loop

    If you realize

    , at least ...

    foreach ($setPositionPercentage as $key =>$val) {
        if ($key! = 'upper') {
            continue;
        }
        for ($i = array_sum ($setPositionPercentage);$i>100;$i-) {
            $setPositionPercentage [$key] + = -1;
        }
    }

    In the first place, if the key you want to update is fixed,
    The outer foreach itself is unnecessary, and it is easier to understand if you write it more simply.

    for statement

    $key = 'upper';
    for ($i = array_sum ($setPositionPercentage);$i>100;$i-) {
        $setPositionPercentage [$key] + = -1;
    }

    While the code is a little shorter.

    $key = 'upper';
    while (array_sum ($setPositionPercentage)>100) {
        $setPositionPercentage [$key] + = -1;
    }

    ★ Bonus ★
    If the key is random and you want to keep "only the first key" -1
    The key can also be specified like this.

    // Get first key of associative array
    $key = key (reset ($setPositionPercentage));