3v4l.org

run code in 500+ PHP versions simultaneously
<?php $arr1 = array( 0 => ['product_type'=>'manufacturer','id'=>1], 1 => ['product_type'=>'manufacturer','id'=>2], 2 => ['product_type'=>'seller'], 3 => ['product_type'=>'seller'], 4 => ['product_type'=>'seller'], 5 => ['product_type'=>'seller'], 6 => ['product_type'=>'seller'], 7 => ['product_type'=>'manufacturer','id'=>3], ); $manuCounter = 0; $sellerCounter = 0; $result = []; foreach ($arr1 as $row) { if ($row['product_type'] === 'manufacturer') { $manuCounter += 4; $result[$manuCounter] = $row; } else { ++$sellerCounter; if (!($sellerCounter % 4)) { ++$sellerCounter; } $result[$sellerCounter] = $row; } } ksort($result); var_export(array_values($result));
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.31, 8.3.0 - 8.3.32, 8.4.1 - 8.4.23, 8.5.0 - 8.5.8
array ( 0 => array ( 'product_type' => 'seller', ), 1 => array ( 'product_type' => 'seller', ), 2 => array ( 'product_type' => 'seller', ), 3 => array ( 'product_type' => 'manufacturer', 'id' => 1, ), 4 => array ( 'product_type' => 'seller', ), 5 => array ( 'product_type' => 'seller', ), 6 => array ( 'product_type' => 'manufacturer', 'id' => 2, ), 7 => array ( 'product_type' => 'manufacturer', 'id' => 3, ), )

preferences:
109.89 ms | 1516 KiB | 4 Q