如何在PHP中创建此数组? [英] How do I create this array in PHP?

查看:53
本文介绍了如何在PHP中创建此数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此查询

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

从这些数据中,如何创建这样的数组

from this data, how do I create an array like this

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基于是1、2还是3,最后每个 product_id 都位于每个数组中。 。

Basically, each step number is a new array and the class_id is either first_class, second_class, third_class based on whether it's 1, 2 or 3 and lastly each product_id is in an array in each ...

任何想法如何创建此数组

Any ideas how to create this array

推荐答案

一个解决方案:

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

现在只需更新即可从数据库中提取。

Now just update to pull from your database.

这篇关于如何在PHP中创建此数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆