php - 我如何以正确的格式获取这些数据?

标签 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

最佳答案

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

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

关于php - 我如何以正确的格式获取这些数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5889777/

相关文章:

java - Android端和php端的HmacSHA1结果不同

php file_get_contents 阿拉伯字符

PHP/JavaScript 隐藏信息

mysql - 用 ASP.NET 编写的 SQL 查询(后面的代码是 VB)未提供预期结果

c++ - 返回一个数组作为指针

arrays - 下标值既不是数组也不是指针错误

php - 如何使用异步脚本获取 tail -f 结果?

php - Mysql 更新查询 PHP 不工作

c# - 如何将 MySQL 数据库数据推送到 MVC .NET Web 应用程序

python - 数据类型为 numpy.ndarray 但预期为 numpy.int64