PHP 数组分组/条件可能吗?
我有一个我自己挖掘的问题。我有一个项目列表,其中有一个字段表示它们适合什么类型的盒子。
例如:
item | boxtype | Quantity
-------------------------------
item1 | A-10,C-20 | 3
item2 | C-10,D-20 | 2
boxtype 列中的字母是盒子类型,后面的数字是可以容纳多少个项目。
我想弄清楚如何使用最少的盒子。我最初只是将数量除以字母数字,如果低于 1,我就使用那个盒子。
但在上述情况下,我会为 item1 使用 Box A,在 item2 上我必须使用 C。最好为这两个项目使用 Box C,因为所有这两个项目都适合。
当然,这只是问题的简化版本,如果需要,我可以用更明确的示例来详细说明。我只是想知道我可以用数组进行所有排序吗?
感谢您在正确方向上的任何一点
编辑 - 添加此数组,从该数组中,是否有一种简单的方法测试字母的条件以确定应使用哪种盒子类型?
Array
(
[AC] => Array
(
[A] => 0.75
[C] => 0.15
)
[CD] => Array
(
[C] => 0.2
[D] => 0.1
)
)
I have a bit of an issue that I dug myself into. I have a list of items that have a field of what type of boxes they will fit in.
For example:
item | boxtype | Quantity
-------------------------------
item1 | A-10,C-20 | 3
item2 | C-10,D-20 | 2
The letter in the boxtype-column is the box type, the number that follows is how many of the item can fit in it.
What I am trying to figure out how to use the least amount of boxes. I originally just divided the quantity by the letter number, if it was under 1 I used that box.
But on cases like the above, where i would use Box A for item1, on item2 I would have to use C. It would be better to use box C for both items since all of both items would fit.
Of course this is just a simplied version of the issue, if needed I could elaborate with more defined example. I was just wondering could I do all the sorting with arrays?
Thanks for any point in the right direction
EDIT - adding this array, from this array, is there an easy way test for conditions of the letters to determine which box type should be used?
Array
(
[AC] => Array
(
[A] => 0.75
[C] => 0.15
)
[CD] => Array
(
[C] => 0.2
[D] => 0.1
)
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来像是一维/线性装箱问题。我不明白它与特定语言(PHP)或功能(数组)有什么关系。对于这些,请举出一个更具体的示例(例如:如何在 PHP 中对这个数组进行排序?
array(blah => blah)
)。对于前者,根本不指定语言,专注于算法。Sounds like a 1D/linear bin packing problem. I don't see what it has to do with a particular language (PHP) or feature (arrays). For those, please come up with a more concrete example (e.g.: How do I sort this array in PHP?
array(blah => blah)
). For the former, do not specify a language at all, focus on the algorithm.