樵夫789
2021-08-14 16:50
采纳率: 66.7%
浏览 46
已结题

PHP 两个数组的交集相减

我有以下两个数组:

原始数组

$a = array('11000','11000','11000','11001','11002','11000','11001','11001');

套装数组 请注意,这里如 11000+11000 这样的组合是存在的。

$b = array('11000+11001:A','11000+11000:B','11001+11001:C');

  • array_diff 不符合我的要求,会去除重复
  • 套装是按$b的key为顺序,从前往后匹配(优先匹配前面的)。如不完全满足套装,则记录下型号。

我希望得到的结果是:array(A, A, A, 11002, 11000)。以下是我的代码,还望各位帮帮忙:

$shopcodeArr = array('11000','11000','11000','11001','11002','11000','11001','11001'); # 原始数组
$dis = array('11000+11001:A','11000+11000:B','11001+11001:C'); # 套装数组
$newcodeArr = array();
$newdis = array();
foreach ($dis as $var) {
    $arr = explode(":", $var);
    $data = array();
    $data['rule'] = explode("+", $arr[0]);
    $data['key'] = $arr[1];
    $newdis[] = $data;
}
function getCode($shopcodeArr, $newdis) {
    static $newcodeArr = array();
    # 开始业务
    foreach ($newdis as $var) {
        # 判断数组是否跑完
        if (!$shopcodeArr) break;
        $data = getCode1($shopcodeArr, $var['rule'], $var['key']);
        $newcodeArr = $data['newcodeArr'];
        $shopcodeArr = $data['shopcodeArr'];
    }
    if (!empty($newcodeArr)) {
        $newcodeArr = array_merge($newcodeArr, $shopcodeArr);
    } else {
        $newcodeArr = $shopcodeArr;
    }
    return $newcodeArr;
}
function getCode1($shopcodeArr, $rule, $key) {
    static $newcodeArr1 = array();
    # 规则数量
    $ruleNum = count($rule);
    # 未删除前的数量
    $codeNum = count($shopcodeArr);
    # 设置为新的数组
    $newshopcodeArr = $shopcodeArr;
    foreach ($rule as $v) {
        unset($newshopcodeArr[array_search($v, $newshopcodeArr)]); 
    }
    # 删除后的数量
    $newcodeNum = count($newshopcodeArr);
    if ($codeNum - $newcodeNum == $ruleNum) {
        # 用删除后的数组替换原数组
        $shopcodeArr = $newshopcodeArr;
        # 表示完全匹配规则
        $newcodeArr1[] = $key;
        # 把剩余的原始数组再次丢入循环里进行再次匹配直至完全无法匹配
           getCode1($shopcodeArr, $rule, $key);
    }
    $data = array(
        'newcodeArr' => $newcodeArr1,
        'shopcodeArr' => $shopcodeArr
    );
    return $data;
}
print_r(getCode($shopcodeArr, $newdis));
// 上述得到的 Array(A, A, A, B, C, 11002, 11000)
// 我想得到的是:array(A, A, A, 11002, 11000)

还望各位帮心解惑一下!

  • 好问题 提建议
  • 收藏

1条回答 默认 最新

  • Stephen_112 2021-08-16 09:11
    已采纳

    虽然不知道你是在干什么,但是还是帮你把问题解决了。

    $shopcodeArr = array('11000', '11000', '11000', '11001', '11002', '11000', '11001', '11001'); # 原始数组
            $dis = array('11000+11001:A', '11000+11000:B', '11001+11001:C'); # 套装数组
            $newcodeArr = array();
            $newdis = array();
            foreach ($dis as $var) {
                $arr = explode(":", $var);
                $data = array();
                $data['rule'] = explode("+", $arr[0]);
                $data['key'] = $arr[1];
                $newdis[] = $data;
            }
            function getCode($shopcodeArr, $newdis)
            {
                static $newcodeArr = array();
                # 开始业务
                foreach ($newdis as $var) {
                    # 判断数组是否跑完
                    if (!$shopcodeArr) break;
                    $data = getCode1($shopcodeArr, $var['rule'], $var['key']);
                    $newcodeArr = $data['newcodeArr'];
                    $shopcodeArr = $data['shopcodeArr'];
                }
                if (!empty($newcodeArr)) {
                    $newcodeArr = array_merge($newcodeArr, $shopcodeArr);
                } else {
                    $newcodeArr = $shopcodeArr;
                }
                return $newcodeArr;
            }
            function getCode1($shopcodeArr, $rule, $key)
            {
                static $newcodeArr1 = array();
                # 规则数量
                $ruleNum = count($rule);
                # 未删除前的数量
                $codeNum = count($shopcodeArr);
                # 设置为新的数组
                $newshopcodeArr = $shopcodeArr;
                foreach ($rule as $v) {
                    unset($newshopcodeArr[array_search($v, $newshopcodeArr)]);
                }
                # 删除后的数量
                $newcodeNum = count($newshopcodeArr);
                if ($codeNum - $newcodeNum == $ruleNum) {
                    # 用删除后的数组替换原数组
                    $shopcodeArr = $newshopcodeArr;
                    # 表示完全匹配规则
                    $newcodeArr1[] = $key;
                    # 把剩余的原始数组再次丢入循环里进行再次匹配直至完全无法匹配
                    $returnData = getCode1($shopcodeArr, $rule, $key);
                    $shopcodeArr = $returnData['shopcodeArr'];
                }
                $data = array(
                    'newcodeArr' => $newcodeArr1,
                    'shopcodeArr' => $shopcodeArr
                );
                return $data;
            }
            print_r(getCode($shopcodeArr, $newdis));
    
    
    评论
    解决 1 无用
    打赏 举报

相关推荐 更多相似问题