3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $arr = array(15, 10, 12, 17, 21, 13, 15, 25, 7, 18, 4, 1, 5, 2); $chunks = array_chunk($arr, 3); $ids = array(); foreach ($chunks as $k => $v) { $otherIds = array(6, 7, 22, 31, 44, 9, 17); $ids = $v; foreach ($chunks as $key => $val) { if ($key <= $k) continue; foreach ($chunks[$key] as $g => $ch) { foreach ($otherIds as $o) { if ($ch['id'] == $o) { $ids[] = $o; unset($chunks[$key][$g]); } } } } } print_r($chunks); print_r($ids);
Output for 5.6.0 - 7.3.0beta1
Array ( [0] => Array ( [0] => 15 [1] => 10 [2] => 12 ) [1] => Array ( [0] => 17 [1] => 21 [2] => 13 ) [2] => Array ( [0] => 15 [1] => 25 [2] => 7 ) [3] => Array ( [0] => 18 [1] => 4 [2] => 1 ) [4] => Array ( [0] => 5 [1] => 2 ) ) Array ( [0] => 5 [1] => 2 )