3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $master_array = array(1, 2, 3, 4, 5, 6); $n = 2; // This is the number of columns / arrays $pattern_array = array(); $meta_array = array(); for ($i=1, $z=2*$n; $i <= $n; ++$i) { $pattern_array[$i] = $i; $pattern_array[$z-i] = $i; $meta_array[] = array(); } foreach ($master_array as $i => $element) { $pattern_index = $i % $z; $meta_index = $pattern_array[$pattern_index] - 1; $meta_array[$meta_index][] = $element; } print_r($meta_array);
Output for 7.1.0
Notice: Use of undefined constant i - assumed 'i' in /in/0L7N3 on line 10 Warning: A non-numeric value encountered in /in/0L7N3 on line 10 Notice: Use of undefined constant i - assumed 'i' in /in/0L7N3 on line 10 Warning: A non-numeric value encountered in /in/0L7N3 on line 10 Notice: Undefined offset: 0 in /in/0L7N3 on line 17 Notice: Undefined offset: 3 in /in/0L7N3 on line 17 Notice: Undefined offset: 0 in /in/0L7N3 on line 17 Array ( [0] => Array ( [0] => 2 [1] => 6 ) [1] => Array ( [0] => 3 ) [-1] => Array ( [0] => 1 [1] => 4 [2] => 5 ) )
Output for 5.3.0 - 5.6.28, 7.0.0 - 7.0.14
Notice: Use of undefined constant i - assumed 'i' in /in/0L7N3 on line 10 Notice: Use of undefined constant i - assumed 'i' in /in/0L7N3 on line 10 Notice: Undefined offset: 0 in /in/0L7N3 on line 17 Notice: Undefined offset: 3 in /in/0L7N3 on line 17 Notice: Undefined offset: 0 in /in/0L7N3 on line 17 Array ( [0] => Array ( [0] => 2 [1] => 6 ) [1] => Array ( [0] => 3 ) [-1] => Array ( [0] => 1 [1] => 4 [2] => 5 ) )
Output for hhvm-3.10.0 - 3.12.0
Notice: Use of undefined constant i - assumed 'i' in /in/0L7N3 on line 10 Notice: Use of undefined constant i - assumed 'i' in /in/0L7N3 on line 10 Notice: Undefined index: 0 in /in/0L7N3 on line 17 Notice: Undefined index: 3 in /in/0L7N3 on line 17 Notice: Undefined index: 0 in /in/0L7N3 on line 17 Array ( [0] => Array ( [0] => 2 [1] => 6 ) [1] => Array ( [0] => 3 ) [-1] => Array ( [0] => 1 [1] => 4 [2] => 5 ) )
Output for 4.3.0 - 5.2.17
Notice: Use of undefined constant i - assumed 'i' in /in/0L7N3 on line 10 Notice: Use of undefined constant i - assumed 'i' in /in/0L7N3 on line 10 Notice: Undefined offset: 0 in /in/0L7N3 on line 17 Notice: Undefined offset: 3 in /in/0L7N3 on line 17 Notice: Undefined offset: 0 in /in/0L7N3 on line 17 Array ( [0] => Array ( [0] => 2 [1] => 6 ) [1] => Array ( [0] => 3 ) [-1] => Array ( [0] => 1 [1] => 4 [2] => 5 ) )