public function tt_add(){ $res = $this->arr_avg(20,14); echo array_sum($res); echo '----' . count($res); dump($res); exit; } /*** * @Param $ amount 'total' * @Param $ count 'number of data need to be split' * @Return array | mixed 'Total amount to break apart the smallest standard standard deviation, average count split into integers' */ public function arr_avg($amount, $count) { $avg_num = $amount / $count; $ceil_num = ceil($amount / $count); if ($ avg_num == $ ceil_num) { $avg_res = array_fill(0, $count, (int)$ceil_num); } else { if ($amount < $count) { avg_res_1 $ = array_fill ( 0 , $ AMOUNT, ( int ) $ ceil_num); // this will only fill 1 $ avg_res_2 = array_fill ($ AMOUNT - 1 , $ COUNT - $ AMOUNT, 0 ); $avg_res = dealed_array_merge($avg_res_1, $avg_res_2); } else { $floor_num = floor($amount / $count); $avg_res = array_fill(0, $count, (int)$floor_num); $left_num = $amount - array_sum($avg_res); if (1 <= $left_num) { $left_avg_res = $this->arr_avg($left_num, $count); foreach ($left_avg_res as $k => $v){ if ($v == 0){ unset($left_avg_res[$k]); } } unset($v); $left_avg_res = array_values($left_avg_res); $avg_res = $this->array_add($avg_res,$left_avg_res); } } } return $avg_res; } /*** * @Param $ a * @param $b * @Return mixed '2 one-dimensional arrays together, adding the same keys, different average retention' */ public function array_add($a,$b) { // keys according to obtain the intersection of two arrays $ ARR = array_intersect_key ($ A, $ B); // iterate second array, if the key name does not exist with the first array, the array elements to a first array of the foreach ($ B AS $ Key => $ value) { IF (! Array_key_exists (Key $, $ a)) { $a[$key] = $value; } } // compute the same array elements and keys, and to replace the original name of the same key array element value corresponding to the foreach ($ ARR AS $ Key => $ value) { $a[$key] = $a[$key] + $b[$key]; } // Returns an array of the added return $ A; }
例:amount:20 count:14
20----8
<pre>array(8) {
[0] => int(3)
[1] => int(3)
[2] => int(3)
[3] => int(3)
[4] => int(2)
[5] => int(2)
[6] => int(2)
[7] => int(2)
}
</pre>
例:amount: 20, count : 20
20----20
<pre>array(20) {
[0] => int(1)
[1] => int(1)
[2] => int(1)
[3] => int(1)
[4] => int(1)
[5] => int(1)
[6] => int(1)
[7] => int(1)
[8] => int(1)
[9] => int(1)
[10] => int(1)
[11] => int(1)
[12] => int(1)
[13] => int(1)
[14] => int(1)
[15] => int(1)
[16] => int(1)
[17] => int(1)
[18] => int(1)
[19] => int(1)
}
</pre>
例: amount: 20, count: 22
20----22
<pre>array(22) {
[0] => int(1)
[1] => int(1)
[2] => int(1)
[3] => int(1)
[4] => int(1)
[5] => int(1)
[6] => int(1)
[7] => int(1)
[8] => int(1)
[9] => int(1)
[10] => int(1)
[11] => int(1)
[12] => int(1)
[13] => int(1)
[14] => int(1)
[15] => int(1)
[16] => int(1)
[17] => int(1)
[18] => int(1)
[19] => int(1)
[20] => int(0)
[21] => int(0)
}
</pre>