3v4l.org

run code in 300+ PHP versions simultaneously
<?php $data = Array ( 0 => Array ('id' => 1, 'check' => ''), 1 => Array ('id' => 2, 'check' => ''), 2 => Array ('id' => 3, 'check' => ''), 3 => Array ('id' => 4, 'check' => ''), 4 => Array ('id' => 5, 'check' => 'on'), 5 => Array ('id' => 6, 'check' => ''), 6 => Array ('id' => 7, 'check' => ''), 7 => Array ('id' => 8, 'check' => ''), 8 => Array ('id' => 9, 'check' => ''), 9 => Array ('id' => 10, 'check' => 'on'), 10 => Array ('id' => 11, 'check' => ''), 11 => Array ('id' => 12, 'check' => ''), 12 => Array ('id' => 13, 'check' => ''), 13 => Array ('id' => 14, 'check' => ''), 14 => Array ('id' => 15, 'check' => 'on'), ); foreach ($data as $key => $value) { if($value['check'] == 'on'){ array_splice($data, $key + 1, 0, array(array())); } } $i=0; foreach ($data as $value) { if (($i % 2) === 0) { echo "<div class='row'>"; } if (isset($value['id'])) { echo '<span>'.$value['id'].'</span>'; } else { echo '<span></span>'; } if (($i % 2) === 1) { echo "</div>"; } $i++;}
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
<div class='row'><span>1</span><span>2</span></div><div class='row'><span>3</span><span>4</span></div><div class='row'><span>5</span><span></span></div><div class='row'><span>6</span><span>7</span></div><div class='row'><span>8</span><span>9</span></div><div class='row'><span></span><span>10</span></div><div class='row'><span>11</span><span>12</span></div><div class='row'><span>13</span><span></span></div><div class='row'><span>14</span><span>15</span></div>

preferences:
162.12 ms | 404 KiB | 206 Q