【问题标题】:How do i get this data in the proper format?我如何以正确的格式获取这些数据?
【发布时间】:2011-08-18 21:17:51
【问题描述】:

好的,我有这个查询

    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;
}

so steps 是包含 9 个元素的完整数组

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

【问题讨论】:

  • 可以通过 foreach() 循环来完成
  • 我假设您通过 mysql_fetch_assoc() 之类的方式获取查询结果?如果您可以向我们展示您目前拥有的代码,则提供具体答案会更容易。
  • 确定我会更新我的问题

标签: php mysql arrays multidimensional-array


【解决方案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

【讨论】:

    【解决方案2】:

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

    我不知道你更喜欢哪一个,但由于你有一个大的连接拆分 SQL,就代码的可读性而言可能不是最差的。性能影响可能会有所不同。

    【讨论】:

      猜你喜欢
      • 2021-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-07
      相关资源
      最近更新 更多