Вот решение:
<?php
$records = array(
array('step_number' => 1, 'class_id' => 1, 'product_id' => 54),
array('step_number' => 1, 'class_id' => 1, 'product_id' => 56),
array('step_number' => 1, 'class_id' => 1, 'product_id' => 57),
array('step_number' => 1, 'class_id' => 1, 'product_id' => 58),
array('step_number' => 1, 'class_id' => 2, 'product_id' => 54),
array('step_number' => 1, 'class_id' => 2, 'product_id' => 56),
array('step_number' => 1, 'class_id' => 2, 'product_id' => 57),
array('step_number' => 2, 'class_id' => 2, 'product_id' => 58),
array('step_number' => 2, 'class_id' => 2, 'product_id' => 59),
array('step_number' => 2, 'class_id' => 2, 'product_id' => 60),
array('step_number' => 2, 'class_id' => 2, 'product_id' => 63),
array('step_number' => 2, 'class_id' => 2, 'product_id' => 64),
);
$new_array = array();
foreach($records as $record => $row) {
$class = '';
switch ($row['class_id']) {
case '1':
$class = 'first_class';
break;
case '2':
$class = 'second_class';
break;
case '3':
$class = 'third_class';
break;
}
$new_array['step_number'][$row['step_number']][$class][] = $row['product_id'];
}
print_r($new_array);
?>
Теперь просто обновитесь, чтобы извлечь из вашей базы данных.