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)); ?>

preferences:
24.43 ms | 405 KiB | 5 Q