3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = Array ( '0' => Array ( 'product_id' => 6, 'brand_id' => 2 ) , '1' => Array ( 'product_id' => 1, 'brand_id' => 1 ) , '2' => Array ( 'product_id' => 5, 'brand_id' => 1 ) ); $key = 'brand_id'; $return = array(); foreach($array as $v) { $return[$v[$key]][] = $v; } print_r(array_values($return)); ?>
Output for 5.6.40, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.31, 8.2.0 - 8.2.27, 8.3.0 - 8.3.15, 8.4.1 - 8.4.2
Array ( [0] => Array ( [0] => Array ( [product_id] => 6 [brand_id] => 2 ) ) [1] => Array ( [0] => Array ( [product_id] => 1 [brand_id] => 1 ) [1] => Array ( [product_id] => 5 [brand_id] => 1 ) ) )

preferences:
138.99 ms | 409 KiB | 5 Q