#php #sql #arrays #multidimensional-array
#php #sql #массивы #многомерный массив
Вопрос:
У меня есть этот запрос
select * from template_product where template_id = 8 group by class_id, product_id
который возвращает
template_product_id template_step_id step_number product_id class_id template
16 11 1 54 1 8
17 11 1 56 1 8
18 11 1 57 1 8
19 11 1 58 1 8
20 11 1 54 2 8
21 11 1 56 2 8
22 11 1 57 2 8
23 11 1 58 2 8
24 11 1 59 2 8
25 11 1 60 2 8
26 11 1 63 2 8
27 11 1 64 2 8
28 11 1 52 2 8
29 11 1 54 3 8
30 11 1 52 3 8
31 12 2 61 1 8
32 12 2 62 2 8
33 12 2 61 3 8
на основе этих данных, как мне создать подобный массив
Array
(
[0] => Array
(
[step_number] => 1
[first_class] => [54, 56, 57, 58]
[second_class] => [54, 56, 57, 58, 59, 60, 63, 64, 52]
[third_class] => [54, 52]
)
[1] => Array
(
[step_number] => 2
[first_class] => [61]
[second_class] => [62]
[third_class] => [61]
)
)
По сути, номер каждого шага представляет собой новый массив, и class_id
это либо first_class
, second_class
, third_class based
зависит от того, равен ли он 1, 2 или 3, и, наконец, каждый из них product_id
находится в массиве в каждом …
Есть идеи, как создать этот массив
Ответ №1:
Вот решение:
<?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);
?>
Теперь просто обновите, чтобы извлечь из вашей базы данных.
Ответ №2:
Должно сработать что-то вроде этого :
$result = array();
$currentStepNumber = null;
foreach ($table as $item)
{
$stepNumber = $item['step_number'];
if ($stepNumber != $currentStepNumber)
{
$result[] = array(
'step_number' => $stepNumber,
'first_class' => array(),
'second_class' => array(),
'third_class' => array()
);
$currentStepNumber = $stepNumber;
}
switch ($item['class_id'])
{
case 1:
$result[count($result) - 1]['first_class'][] = $item['product_id'];
break;
case 2:
$result[count($result) - 1]['second_class'][] = $item['product_id'];
break;
case 3:
$result[count($result) - 1]['third_class'][] = $item['product_id'];
break;
default:
}
}
var_dump($result);
где $table
ваши данные из sql-запроса.
Комментарии:
1. В вашей логике есть риск. Предполагается, что все записи выводятся по порядку step_number или у вас будет несколько массивов с одинаковым step_number. Итак, если вы получите 3 записи, где step_number = 1, 4-ю запись с step_number = 2 и 5-ю с step_number = 1, у вас будет два массива, где step_number = 1.