3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function tryDistributeLinksEvenly($howManyMessages, $links){ echo "testing $howManyMessages\n"; $messages = array_chunk($links, $howManyMessages, true); foreach($messages as $message){ $count = array_reduce(array_keys($message), function($a, $b){ return mb_strlen($a) + mb_strlen($b) + 2; }); var_dump($message); echo $count . "\n"; } echo "-----\n"; } function distributeLinksEvenly($links){ $howManyMessages = 0; do{ $howManyMessages++; $chunks = tryDistributeLinksEvenly($howManyMessages, $links); if($howManyMessages > 10) exit(); }while($chunks === null); return $chunks; } $v = [ 'aaaa', 'bbbb', 'ccccccc', 'dddddddddd', 'eeeeeeee', 'ffffffffffff', 'gggg', 'hhhhhh', 'iiiiiiiiiii', 'lllllll', 'mmmmmmmmmmm', 'nnnnnnn', 'ooo', ]; $v = array_combine($v, $v); distributeLinksEvenly($v);
based on m0Lg4
Output for 5.5.0 - 7.2.0
testing 1 array(1) { ["aaaa"]=> string(4) "aaaa" } 6 array(1) { ["bbbb"]=> string(4) "bbbb" } 6 array(1) { ["ccccccc"]=> string(7) "ccccccc" } 9 array(1) { ["dddddddddd"]=> string(10) "dddddddddd" } 12 array(1) { ["eeeeeeee"]=> string(8) "eeeeeeee" } 10 array(1) { ["ffffffffffff"]=> string(12) "ffffffffffff" } 14 array(1) { ["gggg"]=> string(4) "gggg" } 6 array(1) { ["hhhhhh"]=> string(6) "hhhhhh" } 8 array(1) { ["iiiiiiiiiii"]=> string(11) "iiiiiiiiiii" } 13 array(1) { ["lllllll"]=> string(7) "lllllll" } 9 array(1) { ["mmmmmmmmmmm"]=> string(11) "mmmmmmmmmmm" } 13 array(1) { ["nnnnnnn"]=> string(7) "nnnnnnn" } 9 array(1) { ["ooo"]=> string(3) "ooo" } 5 ----- testing 2 array(2) { ["aaaa"]=> string(4) "aaaa" ["bbbb"]=> string(4) "bbbb" } 7 array(2) { ["ccccccc"]=> string(7) "ccccccc" ["dddddddddd"]=> string(10) "dddddddddd" } 13 array(2) { ["eeeeeeee"]=> string(8) "eeeeeeee" ["ffffffffffff"]=> string(12) "ffffffffffff" } 16 array(2) { ["gggg"]=> string(4) "gggg" ["hhhhhh"]=> string(6) "hhhhhh" } 9 array(2) { ["iiiiiiiiiii"]=> string(11) "iiiiiiiiiii" ["lllllll"]=> string(7) "lllllll" } 11 array(2) { ["mmmmmmmmmmm"]=> string(11) "mmmmmmmmmmm" ["nnnnnnn"]=> string(7) "nnnnnnn" } 11 array(1) { ["ooo"]=> string(3) "ooo" } 5 ----- testing 3 array(3) { ["aaaa"]=> string(4) "aaaa" ["bbbb"]=> string(4) "bbbb" ["ccccccc"]=> string(7) "ccccccc" } 10 array(3) { ["dddddddddd"]=> string(10) "dddddddddd" ["eeeeeeee"]=> string(8) "eeeeeeee" ["ffffffffffff"]=> string(12) "ffffffffffff" } 16 array(3) { ["gggg"]=> string(4) "gggg" ["hhhhhh"]=> string(6) "hhhhhh" ["iiiiiiiiiii"]=> string(11) "iiiiiiiiiii" } 14 array(3) { ["lllllll"]=> string(7) "lllllll" ["mmmmmmmmmmm"]=> string(11) "mmmmmmmmmmm" ["nnnnnnn"]=> string(7) "nnnnnnn" } 11 array(1) { ["ooo"]=> string(3) "ooo" } 5 ----- testing 4 array(4) { ["aaaa"]=> string(4) "aaaa" ["bbbb"]=> string(4) "bbbb" ["ccccccc"]=> string(7) "ccccccc" ["dddddddddd"]=> string(10) "dddddddddd" } 14 array(4) { ["eeeeeeee"]=> string(8) "eeeeeeee" ["ffffffffffff"]=> string(12) "ffffffffffff" ["gggg"]=> string(4) "gggg" ["hhhhhh"]=> string(6) "hhhhhh" } 9 array(4) { ["iiiiiiiiiii"]=> string(11) "iiiiiiiiiii" ["lllllll"]=> string(7) "lllllll" ["mmmmmmmmmmm"]=> string(11) "mmmmmmmmmmm" ["nnnnnnn"]=> string(7) "nnnnnnn" } 11 array(1) { ["ooo"]=> string(3) "ooo" } 5 ----- testing 5 array(5) { ["aaaa"]=> string(4) "aaaa" ["bbbb"]=> string(4) "bbbb" ["ccccccc"]=> string(7) "ccccccc" ["dddddddddd"]=> string(10) "dddddddddd" ["eeeeeeee"]=> string(8) "eeeeeeee" } 12 array(5) { ["ffffffffffff"]=> string(12) "ffffffffffff" ["gggg"]=> string(4) "gggg" ["hhhhhh"]=> string(6) "hhhhhh" ["iiiiiiiiiii"]=> string(11) "iiiiiiiiiii" ["lllllll"]=> string(7) "lllllll" } 11 array(3) { ["mmmmmmmmmmm"]=> string(11) "mmmmmmmmmmm" ["nnnnnnn"]=> string(7) "nnnnnnn" ["ooo"]=> string(3) "ooo" } 7 ----- testing 6 array(6) { ["aaaa"]=> string(4) "aaaa" ["bbbb"]=> string(4) "bbbb" ["ccccccc"]=> string(7) "ccccccc" ["dddddddddd"]=> string(10) "dddddddddd" ["eeeeeeee"]=> string(8) "eeeeeeee" ["ffffffffffff"]=> string(12) "ffffffffffff" } 16 array(6) { ["gggg"]=> string(4) "gggg" ["hhhhhh"]=> string(6) "hhhhhh" ["iiiiiiiiiii"]=> string(11) "iiiiiiiiiii" ["lllllll"]=> string(7) "lllllll" ["mmmmmmmmmmm"]=> string(11) "mmmmmmmmmmm" ["nnnnnnn"]=> string(7) "nnnnnnn" } 11 array(1) { ["ooo"]=> string(3) "ooo" } 5 ----- testing 7 array(7) { ["aaaa"]=> string(4) "aaaa" ["bbbb"]=> string(4) "bbbb" ["ccccccc"]=> string(7) "ccccccc" ["dddddddddd"]=> string(10) "dddddddddd" ["eeeeeeee"]=> string(8) "eeeeeeee" ["ffffffffffff"]=> string(12) "ffffffffffff" ["gggg"]=> string(4) "gggg" } 8 array(6) { ["hhhhhh"]=> string(6) "hhhhhh" ["iiiiiiiiiii"]=> string(11) "iiiiiiiiiii" ["lllllll"]=> string(7) "lllllll" ["mmmmmmmmmmm"]=> string(11) "mmmmmmmmmmm" ["nnnnnnn"]=> string(7) "nnnnnnn" ["ooo"]=> string(3) "ooo" } 7 ----- testing 8 array(8) { ["aaaa"]=> string(4) "aaaa" ["bbbb"]=> string(4) "bbbb" ["ccccccc"]=> string(7) "ccccccc" ["dddddddddd"]=> string(10) "dddddddddd" ["eeeeeeee"]=> string(8) "eeeeeeee" ["ffffffffffff"]=> string(12) "ffffffffffff" ["gggg"]=> string(4) "gggg" ["hhhhhh"]=> string(6) "hhhhhh" } 9 array(5) { ["iiiiiiiiiii"]=> string(11) "iiiiiiiiiii" ["lllllll"]=> string(7) "lllllll" ["mmmmmmmmmmm"]=> string(11) "mmmmmmmmmmm" ["nnnnnnn"]=> string(7) "nnnnnnn" ["ooo"]=> string(3) "ooo" } 7 ----- testing 9 array(9) { ["aaaa"]=> string(4) "aaaa" ["bbbb"]=> string(4) "bbbb" ["ccccccc"]=> string(7) "ccccccc" ["dddddddddd"]=> string(10) "dddddddddd" ["eeeeeeee"]=> string(8) "eeeeeeee" ["ffffffffffff"]=> string(12) "ffffffffffff" ["gggg"]=> string(4) "gggg" ["hhhhhh"]=> string(6) "hhhhhh" ["iiiiiiiiiii"]=> string(11) "iiiiiiiiiii" } 14 array(4) { ["lllllll"]=> string(7) "lllllll" ["mmmmmmmmmmm"]=> string(11) "mmmmmmmmmmm" ["nnnnnnn"]=> string(7) "nnnnnnn" ["ooo"]=> string(3) "ooo" } 7 ----- testing 10 array(10) { ["aaaa"]=> string(4) "aaaa" ["bbbb"]=> string(4) "bbbb" ["ccccccc"]=> string(7) "ccccccc" ["dddddddddd"]=> string(10) "dddddddddd" ["eeeeeeee"]=> string(8) "eeeeeeee" ["ffffffffffff"]=> string(12) "ffffffffffff" ["gggg"]=> string(4) "gggg" ["hhhhhh"]=> string(6) "hhhhhh" ["iiiiiiiiiii"]=> string(11) "iiiiiiiiiii" ["lllllll"]=> string(7) "lllllll" } 11 array(3) { ["mmmmmmmmmmm"]=> string(11) "mmmmmmmmmmm" ["nnnnnnn"]=> string(7) "nnnnnnn" ["ooo"]=> string(3) "ooo" } 7 ----- testing 11 array(11) { ["aaaa"]=> string(4) "aaaa" ["bbbb"]=> string(4) "bbbb" ["ccccccc"]=> string(7) "ccccccc" ["dddddddddd"]=> string(10) "dddddddddd" ["eeeeeeee"]=> string(8) "eeeeeeee" ["ffffffffffff"]=> string(12) "ffffffffffff" ["gggg"]=> string(4) "gggg" ["hhhhhh"]=> string(6) "hhhhhh" ["iiiiiiiiiii"]=> string(11) "iiiiiiiiiii" ["lllllll"]=> string(7) "lllllll" ["mmmmmmmmmmm"]=> string(11) "mmmmmmmmmmm" } 15 array(2) { ["nnnnnnn"]=> string(7) "nnnnnnn" ["ooo"]=> string(3) "ooo" } 6 -----