如何以正确的格式获取此数据?

时间:2011-05-04 20:57:55

标签: php mysql arrays multidimensional-array

好的,所以我有这个查询

    select ss.system_step_id, ss.step_number, cd.name, ssp.system_step_product_id, p.cost, ssp.class_id from system_step ss 
    join system as s on s.system_id=ss.system_id 
    join category_description as cd on cd.category_id=ss.sub_category_id 
    join system_step_product as ssp on ss.system_step_id=ssp.system_step_id 
    join product as p on p.product_id=ssp.product_id where s.system_id = 41 
    order by ss.step_number, ssp.class_id;

which yields this result

7   1   Screens         808 115.0000 1
7   1   Screens         809 381.9000 2
7   1   Screens         810 441.9000 3
8   2   Printers        811 112.3200 1
8   2   Printers        812 201.0400 2
8   2   Printers        813 202.8700 3
9   3   Cash Drawers    814 135.7000 1
9   3   Cash Drawers    815 86.5400  2
9   3   Cash Drawers    816 135.7000 3

有没有办法把它变成这样的三个元素的php数组

Array
(
    [0] => Array
        (
            [name] => "Screens"
            [standard_product] => Array ([id] => 808, [price] => '115.0000')
            [business_product] => Array ([id] => 809, [price] => '381.9000')
            [premium_product] => Array ([id] => 810, [price] => '441.9000') 
        )                      

    [1] => Array               
        (                      
          [name] => "Printers"
          [standard_product] => Array ([id] => 811, [price] => '112.3200')
          [business_product] => Array ([id] => 812, [price] => '201.0400')
          [premium_product] => Array ([id] => 813, [price] => '202.8700')
        )
    [2] => Array               
        (                      
          [name] => "Cash Drawers"
          [standard_product] => Array ([id] => 814, [price] => '135.7000')
          [business_product] => Array ([id] => 815, [price] => '86.5400')
          [premium_product] => Array ([id] => 816, [price] => '135.7000')
        )
)


$sql = "select ss.system_step_id, ss.step_number, cd.name, ssp.system_step_product_id, p.cost, ssp.class_id, pd.name as product_name, pd.description from system_step ss join system as s on s.system_id=ss.system_id join category_description as cd on cd.category_id=ss.sub_category_id join system_step_product as ssp on ss.system_step_id=ssp.system_step_id join product as p on p.product_id=ssp.product_id join product_description as pd on pd.product_id=p.product_id where s.system_id = {$system['system_id']} order by ss.step_number, ssp.class_id;";
$result = mysql_query($sql);
$steps = array();
while($row_r = mysql_fetch_assoc($result)){
  $steps[] = $row_r;
}

所以步骤是包含9个元素的完整数组

正如您所看到的,唯一需要注意的是class_id 1是standard_product class_id 2是business_product而class_id 3是premium_product

2 个答案:

答案 0 :(得分:1)

您可以在获取SQL结果时获取填充多维数组或拆分为多个SQL命令,从而构建数组(第一个获取名称,而不是具有每个名称值的产品)。

我不知道你更喜欢哪个,但是因为你有一个大的连接分裂SQL可能不是你的代码的可读性最差。性能影响可能会有所不同。

答案 1 :(得分:1)

这可以做到这一点

<?php

    $finalArray = array();
    $nameArray = array('Screens', 'Printers', 'Cash Drawers');

    foreach ($nameArray as $name) {

        while ($row = mysql_fetch_assoc($result)) {

            if ($row['name'] == $name) {

                if ($row['class_id'] == 1) {

                    $standard = array('id' => $row['system_step_product_id'], 'price' => $row['cost']);
                }
                else if ($row['class_id'] == 2) {

                    $business = array('id' => $row['system_step_product_id'], 'price' => $row['cost']);
                }
                else if ($row['class_id'] == 3) {

                    $premium = array('id' => $row['system_step_product_id'], 'price' => $row['cost']);
                }
            }
        }

        array_push($finalArray, array('name' => $name, 'standard_product' => $standard, 'business_product' => $business, 'premium_product' => $premium,));
    }

?>

希望我的所有列名都正确。

这是我测试它的,我得到了正确的输出http://codepad.org/yjU3gxbB

您唯一可以改变的是动态创建名称数组

    while ($row = mysql_fetch_assoc($result)) {

        array_push($nameArray, $row['name']);
    }

    $nameArray = array_unique($nameArray);

但是,当您在同一查询中执行mysql_fetch_assoc两次时,PHP不喜欢它。您可能想要进行另一个查询,只选择cd.name