3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function nextAssoc($assoc) { if(false !== ($pos = strrpos($assoc, '01'))) { $assoc[$pos] = '1'; $assoc[$pos+1] = '0'; return substr($assoc, 0, $pos+2). str_repeat('0', substr_count(substr($assoc, $pos+2), '0')). str_repeat('1', substr_count(substr($assoc, $pos+2), '1')); } return false; } function getAssoc(array $data, $count=2, $split=true) { if(count($data)<$count) { return null; } $assoc = str_repeat('0', count($data)-$count).str_repeat('1', $count); $result = []; do { $result[]=array_intersect_key($data, array_filter(str_split($assoc))); } while($assoc=nextAssoc($assoc)); return $split?array_chunk($result, (int)(count($data)/$count)):$result; } $data = [1,2,3,4,5,6]; print_r(getAssoc($data, 2)); print_r(getAssoc($data, 3));
Output for 5.4.0 - 7.1.0
Array ( [0] => Array ( [0] => Array ( [4] => 5 [5] => 6 ) [1] => Array ( [3] => 4 [5] => 6 ) [2] => Array ( [3] => 4 [4] => 5 ) ) [1] => Array ( [0] => Array ( [2] => 3 [5] => 6 ) [1] => Array ( [2] => 3 [4] => 5 ) [2] => Array ( [2] => 3 [3] => 4 ) ) [2] => Array ( [0] => Array ( [1] => 2 [5] => 6 ) [1] => Array ( [1] => 2 [4] => 5 ) [2] => Array ( [1] => 2 [3] => 4 ) ) [3] => Array ( [0] => Array ( [1] => 2 [2] => 3 ) [1] => Array ( [0] => 1 [5] => 6 ) [2] => Array ( [0] => 1 [4] => 5 ) ) [4] => Array ( [0] => Array ( [0] => 1 [3] => 4 ) [1] => Array ( [0] => 1 [2] => 3 ) [2] => Array ( [0] => 1 [1] => 2 ) ) ) Array ( [0] => Array ( [0] => Array ( [3] => 4 [4] => 5 [5] => 6 ) [1] => Array ( [2] => 3 [4] => 5 [5] => 6 ) ) [1] => Array ( [0] => Array ( [2] => 3 [3] => 4 [5] => 6 ) [1] => Array ( [2] => 3 [3] => 4 [4] => 5 ) ) [2] => Array ( [0] => Array ( [1] => 2 [4] => 5 [5] => 6 ) [1] => Array ( [1] => 2 [3] => 4 [5] => 6 ) ) [3] => Array ( [0] => Array ( [1] => 2 [3] => 4 [4] => 5 ) [1] => Array ( [1] => 2 [2] => 3 [5] => 6 ) ) [4] => Array ( [0] => Array ( [1] => 2 [2] => 3 [4] => 5 ) [1] => Array ( [1] => 2 [2] => 3 [3] => 4 ) ) [5] => Array ( [0] => Array ( [0] => 1 [4] => 5 [5] => 6 ) [1] => Array ( [0] => 1 [3] => 4 [5] => 6 ) ) [6] => Array ( [0] => Array ( [0] => 1 [3] => 4 [4] => 5 ) [1] => Array ( [0] => 1 [2] => 3 [5] => 6 ) ) [7] => Array ( [0] => Array ( [0] => 1 [2] => 3 [4] => 5 ) [1] => Array ( [0] => 1 [2] => 3 [3] => 4 ) ) [8] => Array ( [0] => Array ( [0] => 1 [1] => 2 [5] => 6 ) [1] => Array ( [0] => 1 [1] => 2 [4] => 5 ) ) [9] => Array ( [0] => Array ( [0] => 1 [1] => 2 [3] => 4 ) [1] => Array ( [0] => 1 [1] => 2 [2] => 3 ) ) )
Output for 5.1.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/vJJJn on line 23
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_ARRAY, expecting '&' or T_VARIABLE in /in/vJJJn on line 16
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_ARRAY, expecting ')' in /in/vJJJn on line 16
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_ARRAY, expecting ')' in /in/vJJJn on line 16
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `')'' in /in/vJJJn on line 16
Process exited with code 255.