doujimiao7480 2018-11-08 08:35
浏览 58

使用数组和PHP查找匹配的相等订单

I am cracking my brain and can't find a good solution for my problem. I am trying to design a system that I can use for batch picking in our order system.

The point is that from a set of orders I want to pick 6 orders that are most equal to each other. In our warehouse most orders are them so we can safe a lot of time by picking some orders at the same time.

Assume I have the following array:

<?php

$data = [
    156 => [
        1,
        2,
        7,
        9,
    ],
    332 => [
        3,
        10,
        6
    ],
    456 => [
        1,
    ],
    765 => [
        7,
        2,
        10,
    ],
    234 => [
        1,
        9,
        3,
        6,
    ],
    191 => [
        7,
    ],
    189 => [
        7,
        6,
        3,
    ],
    430 => [
        10,
        9,
        1,
    ],
    482 => [
        1,
        2,
        7,
    ],
    765 => [
        1,
        5,
        9,
    ]
];
?>

The array key is the order id, and the values are the product ID's it contains. If I want to pick the top 3 orders which look at much like each other, where do I start?

Any help would be much appreciated!

  • 写回答

2条回答 默认 最新

  • dougu1045 2018-11-08 09:03
    关注

    Here is what i would do if I had the problem :

    $topOrders = [];
    foreach($data as $value):
        foreach($value as $order):
            if(isset($$order)):
                $$order++;
            else:
                $$order = 1;
            endif;
            $topOrders[$order] = $$order;
        endforeach;
    endforeach;
    
    print_r($topOrders);
    

    In $topOrders, you have an array that contains as key the ID, and as value you got the number of orders. All you have to do is to sort your array to get your top 3.

    评论

报告相同问题?