【问题标题】:group same name from array and show total of an indivisual group从数组中分组同名并显示单个组的总数
【发布时间】:2017-12-30 01:26:47
【问题描述】:

我有一个如下所示的数组:

    Array
    (
        [0] => stdClass Object
            (
                [products_name] => Parla to gold(Flower)
                [measurement] => 18
                [unit] => mm
                [products_size_height] => 0
                [products_size_width] => 0
                [products_size_unit] => inch
                [products_type] => 4
                [products_thickness_measurement] => 18
                [product_ordered_pcs] => 4
                [product_ordered_quantity] => 100
                [others_feature] => 
                [rate] => 500
                [amount] => 50000
            )

        [1] => stdClass Object
            (
                [products_name] => Parla to gold(Flower)
                [measurement] => 18
                [unit] => mm
                [products_size_height] => 0
                [products_size_width] => 0
                [products_size_unit] => inch
                [products_type] => 4
                [products_thickness_measurement] => 18
                [product_ordered_pcs] => 0
                [product_ordered_quantity] => 45
                [others_feature] => 
                [rate] => 45
                [amount] => 2025
            )

        [2] => stdClass Object
            (
                [products_name] => Parla to gold(Flower)
                [measurement] => 18
                [unit] => mm
                [products_size_height] => 0
                [products_size_width] => 0
                [products_size_unit] => inch
                [products_type] => 2
                [products_thickness_measurement] => 18
                [product_ordered_pcs] => 4
                [product_ordered_quantity] => 100
                [others_feature] => 
                [rate] => 850
                [amount] => 85000
            )
.......

现在我想对同一个产品进行分组(产品将通过 products_name、measurement 和 products_type 的组合来识别)以一次显示一个产品,并对同一产品的“product_ordered_quantity”进行总计。看图你就明白我想说什么了。我将生成 PDF 文件,所以请不要使用 javascript。

结果:

我想要:

【问题讨论】:

  • 你尝试过做什么?请发布一些您至少尝试过的证据
  • 您可以制作数组并通过填充器或映射 php 函数将您的产品分组到该数组中......而不仅仅是显示您想要的数据
  • 我有一个从 laravel 集合中获得的数组,现在我想在表格上显示与第二张图片完全相同的结果。
  • 如果您从 db 查询中获取此数据,则更容易实现您所需要的。如果您确认它来自对数据库的查询,我可以为您提供答案
  • 是的,它来自对 DB @gbalduzzi 的查询

标签: php html arrays laravel


【解决方案1】:

如果这是来自数据库的查询,您可以使用 Collection 类 (https://laravel.com/docs/5.4/eloquent-collections) 提供的一些方法

$query; //The query object BEFORE getting the data, so before get() or paginate()
$data = $query->selectRaw('sum(product_ordered_quantity) as quantity')
              ->groupBy('products_name','measurement', 'products_type')
              ->get();

【讨论】:

  • 我得到了总数量,但是 Length x Width 呢?它只显示一个长度 x 宽度。有什么办法可以证明这一点吗?我有多个相同产品的长度 x 宽度。请帮助@gbalduzzi
  • 我认为你需要做 2 个查询,一个用于组,一个用于单行
  • 谢谢兄弟,我从你的回答中得到了想法,然后我解决了它
【解决方案2】:

你可以试试这个漂亮的类,它可以让你像查询数据库一样查询数组 - https://phplinq.codeplex.com/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-05
    • 2020-11-13
    • 2020-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多