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.

    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?