duannue2455 2016-07-06 15:42
浏览 7
已采纳

减少和组合数组

I am trying to create an array that succinctly gives me the following:

  • ID of a component part
  • ID of the supplier for that component part
  • The volume breakpoints, and their associated unit costs

I am using Laravel 5.2, although this is a more general PHP question.

So, I have a database table that looks like this:

sql screenshot

I have a function, as below, to get some prices of components:

    public function get_component_prices()
    {
        $components = DB::table('component_supplier')
            ->select('component_id', 'supplier_id', 'volume', 'unit_cost')
            ->get();

        $prices = [];

        foreach ($components as $component) {
            array_push($prices, ["component_id" => $component->component_id, "supplier_id" => $component->supplier_id, "volumes" => [$component->volume => $component->unit_cost]]);
        }

        dd($prices);
    }

This gives me the array:

    array:7 [▼
  0 => array:3 [▼
    "component_id" => 3
    "supplier_id" => 1
    "volumes" => array:1 [▼
      100 => "1.5000"
    ]
  ]
  1 => array:3 [▼
    "component_id" => 3
    "supplier_id" => 1
    "volumes" => array:1 [▼
      207 => "1.0100"
    ]
  ]
  2 => array:3 [▼
    "component_id" => 3
    "supplier_id" => 1
    "volumes" => array:1 [▼
      500 => "0.8000"
    ]
  ]
  3 => array:3 [▼
    "component_id" => 3
    "supplier_id" => 1
    "volumes" => array:1 [▼
      1000 => "0.4000"
    ]
  ]
  4 => array:3 [▼
    "component_id" => 3
    "supplier_id" => 2
    "volumes" => array:1 [▼
      10000 => "0.2000"
    ]
  ]
  5 => array:3 [▼
    "component_id" => 4
    "supplier_id" => 2
    "volumes" => array:1 [▼
      100 => "0.1000"
    ]
  ]
  6 => array:3 [▼
    "component_id" => 4
    "supplier_id" => 2
    "volumes" => array:1 [▼
      500 => "0.0700"
    ]
  ]
]

You can see that certain suppliers and components have multiple volumes.

Therefore, I'd like to try to group the array a bit better, combining the repeated parts - perhaps like so, for example:

6 => array:3 [▼
        "component_id" => 4
        "supplier_id" => 2
        "volumes" => array:3 [▼
          100 => "0.1000",
          500 => "0.0700"
        ]
      ]

So that for each component_id and supplier_id group, there's a set of 'volumes'.

Any advice is much appreciated... I have been trying for hours to get the array sorted!

  • 写回答

1条回答 默认 最新

  • duande1986 2016-07-06 16:24
    关注

    In Mysql, you can do something like this

    SELECT component_id, supplier_id,
           GROUP_CONCAT(volume, ':', unit_cost) AS volumes
    FROM component_supplier
    GROUP BY CONCAT(component_id, supplier_id)
    

    http://sqlfiddle.com/#!9/cb7bb/1

    Output of the above query

    Then you can simply loop this query in PHP and explode volumes field by the comma.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计