首页 > 解决方案 > 检查一个数组的值是否与另一个数组的键匹配 - PHP

问题描述

我有两个数组:

<?php

$arr1 = ["","2nd Level","11' Ceilings","1016 SF","1168SF","1186-1204SF","11ft Ceilings","1211-1226SF","14 Square Footage","15 Square Footage","16 Square Footage","17 Square Footage","18 Square Footage","19 Square Footage","20 Square Footage","21 Square Footage","2nd","3rd","4th","538-541SF","543-564SF","547-557SF","5th","614-619SF","629-639SF","6th","701-724SF","7th Fl PH1A","8th Street View","905-1046SF","Additional Closets","Alley View","Alley View 2","Alley\/Wall View 65 Stack","Bradley","City View","Corner Unit","Courtyard View","Dbl Balcony","Double Windows","Eisen 1","Eisen 2","Eisen 3","Eisen 4","End Unit","Goodwin","Grand Ave View","Int Corner 2","Interior Corner","Large Storage","Lg. Balcony","Loss Leader","Loss Leader2","Loss Leader3","Loss Leader4","Nielson 1","Nielson 2","Nielson 3","Nielson 4","Nielson 5","Nielson 6","Olive Street View","Partial Courtyard View","Partial Pool View","Patio","Ph1a","Ph1b","Ph2","Ph3","Pol. Concrete","Pool View","Regular Balcony","Rent","Sm. Balcony","Small Storage","Top Floor","View 4","View 5","Walk-in Closet","Walker","murphy bed"];

$arr2 = array (
  '1st Floor' => 375,
  '2nd Floor' => 375,
  '3rd Floor' => 375,
  '4th Floor' => 375,
  '5th Floor' => 375,
  'Fireplace' => 384,
  'RPRenovation' => 380,
  'Renovated' => 380,
  'Rent' => 381,
  'Test' => 383,
  'FEAT-ADA' => 368,
  'FEAT-GAR 1C' => 384,
  'FEAT-W/D' => 384,
  'FEAT-YARD' => 384,
  'FL-LC--1B' => 375,
  'FL-LC--2B' => 375,
  'FL-LC-All-1B' => 375,
  'FL-LC-All-2B' => 375,
  'FL-P1--1B' => 375,
  'FL-P1--2B' => 375,
  'LOC-NEG' => 379,
  'LOC-OTHER' => 379,
  'LVL-1ST-1B' => 375,
  'LVL-1ST-2B' => 375,
  'LVL-TOP-1B' => 375,
  'LVL-TOP-2B' => 375,
  'R-LC-DMD-1B' => 375,
  'R-LC-DMD-2B' => 379,
  'R-LC-GLD-1B' => 379,
  'R-LC-GLD-2B' => 379,
  'R-LC-PLT-1B' => 379,
  'R-LC-PLT-2B' => 379,
  'base rent adj' => 381,
  'Balcony' => 370,
  '1/2 Bath' => 371,
  '2018 Bathroom Upgrade' => 371,
  '2018 Kitchen Upgrade' => 378,
  '2018 Laminate Countertops' => 377,
  '2018 Stainless Steel Appliance' => 378,
  '2018 Subway Tile' => 377,
  '2018 Vinyl Plank Flooring' => 377,
  'Accessible Unit' => 368,
  'Bonus Room' => 382,
  'Bottom Floor' => 375,
  'Building B' => 379,
  'Building D' => 379,
  'Building E' => 379,
  'Bump Out' => 384,
  'Cinderblock Walls' => 384,
  'Corner unit' => 374,
  'Electric Included With Rent' => 381,
  'Extra Storage' => 373,
  'French Doors' => 384,
  'Hardwood Floors' => 377,
  'Laundry Room' => 379,
  'Non-Operable Windows' => 387,
  'Platform with Storage' => 373,
  'Square Feet: 1000 (1BD)' => 382,
  'Square Feet: 1008 (1BD)' => 382,
  'Square Feet: 1030 (1BD)' => 382,
  'Square Feet: 1108 (2BD)' => 382,
  'Square Feet: 1110 (2BD)' => 382,
  'Square Feet: 1121 (2BD)' => 382,
  'Square Feet: 1123 (1BD)' => 382,
  'Square Feet: 1130 (2BD)' => 382,
  'Square Feet: 1170 (2BD)' => 382,
  'Square Feet: 1175 (2BD) Base' => 382,
  'Square Feet: 1220 (2BD)' => 382,
  'Square Feet: 1237 (2BD)' => 382,
  'Square Feet: 1280 (2BD)' => 382,
  'Square Feet: 1309 (2BD)' => 382,
  'Square Feet: 1342 (2BD)' => 382,
  'Square Feet: 580 (1BD)' => 382,
  'Square Feet: 583 (1BD)' => 382,
  'Square Feet: 691 (1BD)' => 382,
  'Square Feet: 696 (1BD)' => 382,
  'Square Feet: 740 (1BD)' => 382,
  'Square Feet: 748 (1BD)' => 382,
  'Square Feet: 789 (1BD)' => 382,
  'Square Feet: 792 (1BD)' => 382,
  'Square Feet: 805 (1BD) Base' => 382,
  'Square Feet: 847 (1BD)' => 382,
  'Square Feet: 848 (2BD)' => 382,
  'Square Feet: 880 (1BD)' => 382,
  'Square Feet: 912 (1BD)' => 382,
  'Square Feet: 974 (1BD)' => 382,
  'Square Feet: 987 (1BD)' => 382,
  'Stainless steel appliances' => 378,
  'Standard Townhome' => 383,
  'Top Floor' => 375,
  'View - Motorcycle Parking' => 386,
  'View - Parking' => 386,
  'View - Partial River' => 386,
  'View - Railroad Tracks' => 386,
  'View - River' => 386,
  'View - Service Area' => 386,
  'Windows Do Not Open' => 387,
  'Ada' => 368,
  'accessible' => 368,
  'handicap' => 368,
  'Affordable' => 369,
  'BMR' => 369,
  'Bond' => 369,
  'HAP' => 369,
  'balconies' => 370,
  'Patio' => 370,
  'Terrace' => 370,
  'Lanai' => 370,
  'Bathroom' => 371,
  'bathrooms' => 371,
  'Shower' => 371,
  'showers' => 371,
  'Vanity' => 371,
  'sink' => 371,
  'Ceiling' => 372,
  'ceilings' => 372,
  'vaulted' => 372,
  'Storage' => 373,
  'Closet' => 373,
  'closets' => 373,
  'Walk-in' => 373,
  'Linen' => 373,
  'Corner' => 374,
  'corners' => 374,
  'Level' => 375,
  'levels' => 375,
  '1st' => 375,
  '2nd' => 375,
  '3rd' => 375,
  '4th' => 375,
  '5th' => 375,
  '6th' => 375,
  '7th' => 375,
  '8th' => 375,
  '9th' => 375,
  '10th' => 375,
  '11th' => 375,
  '12th' => 375,
  '13th' => 375,
  '14th' => 375,
  '15th' => 375,
  '16th' => 375,
  '17th' => 375,
  '18th' => 375,
  '19th' => 375,
  '20th' => 375,
  'Layout' => 376,
  'layouts' => 376,
  'floor plan' => 376,
  'floorplan' => 376,
  'Finish' => 377,
  'finishes' => 377,
  'Flooring' => 377,
  'Wood' => 377,
  'Plank' => 377,
  'Vinyl' => 377,
  'Kitchen' => 378,
  'kitchens' => 378,
  'Stainless' => 378,
  'Appliance' => 378,
  'appliances' => 378,
  'Location' => 379,
  'locations' => 379,
  'End' => 379,
  'Reno' => 380,
  'renovation' => 380,
  'renovations' => 380,
  'Platinum' => 380,
  'Silver' => 380,
  'Gold' => 380,
  'Upgrade' => 380,
  'upgrades' => 380,
  'Rents' => 381,
  'Discount' => 381,
  'Loss Leader' => 381,
  'SF' => 382,
  'sqft' => 382,
  'sq ft' => 382,
  'square feet' => 382,
  'square foot' => 382,
  'sq feet' => 382,
  'square footage' => 382,
  'Feature' => 384,
  'features' => 384,
  'W/D' => 384,
  'WD' => 384,
  'Washer' => 384,
  'Dryer' => 384,
  'Garage' => 384,
  'Garages' => 384,
  'Driveway' => 384,
  'view' => 386,
  'views' => 386,
  'Window' => 387,
  'windows' => 387,
);

要求:

检查条件

1)检查是否有任何值$arr1在 的键中$arr2。2)如果值不完全匹配,我们需要从值中拆分单词$arr1并检查所有单词是否匹配并且具有相同的值。

即,例如

  1. 检查是否2nd level与中的任何键匹配$arr2
  2. 如果2nd level不罚款任何匹配拆分2nd level2ndand level。如果两者都2nd匹配level,则与具有相同值的键匹配。

在这种情况下,两者2nd都有level值,375这意味着这应该被认为是匹配的。如果2ndhad375levelhad 376,这将不匹配。

如果匹配:

上面我提到了两种可以认为是匹配的方式,如果找到匹配,则将结果写入$result索引为的新数组中$arr1

例如:如果$res[0]找不到匹配项,则$res[0] = ''. 同样 `$res[1] = '375' 等等。

例如:$res数组可能是:

$res = array (
  0 => '',
  1 => '375',
  2 => '',
  3 => '',
  4 => '',
  5 => '',
  6 => '',
  7 => '',
  8 => '',
  9 => '',
  10 => '',
  11 => '',
  12 => '',
  13 => '',
  14 => '',
  15 => '',
  16 => 375,
  17 => 375,
  18 => 375,
  19 => '',
  20 => '',
  21 => '',
  22 => 375,
  23 => '',
  24 => '',
  25 => 375,
  26 => '',
  27 => '',
  28 => '',
  29 => '',
  30 => '',
  31 => '',
  32 => '',
  33 => '',
  34 => '',
  35 => '',
  36 => 374,
  37 => '',
  38 => '',
  39 => '',
  40 => '',
  41 => '',
  42 => '',
  43 => '',
  44 => '',
  45 => '',
  46 => '',
  47 => '',
  48 => '',
  49 => '',
  50 => '',
  51 => 381,
  52 => '',
  53 => '',
  54 => '',
  55 => '',
  56 => '',
  57 => '',
  58 => '',
  59 => '',
  60 => '',
  61 => '',
  62 => '',
  63 => '',
  64 => 370,
  65 => '',
  66 => '',
  67 => '',
  68 => '',
  69 => '',
  70 => '',
  71 => '',
  72 => 381,
  73 => '',
  74 => '',
  75 => 375,
  76 => '',
  77 => '',
  78 => '',
  79 => '',
  80 => '',
)

笔记:

$res数组可能与显示的不同,因为这只是假设,但格式应该是这样的。此外,我们可以忽略大小写敏感。

#

基本上,对于第一次检查,我可以通过循环两个 foreach 数组来做到这一点:

$res = array();
foreach($arr1 as $ak => $av){
    $match = false;
    foreach($arr2 as $k => $v){
        if(!empty($av) && !empty($k)){
            if (preg_match("`\b" . preg_quote($av) . "\b`i", $k)) {
                array_push($res, $ar);
                $match= true;
                break;
            }    
        }
    }
}
if(!$match) {
    array_push($res, '');
}

这适用于检查条件 1,但需要为检查 2 添加,但是,我不确定这是否是有效的方法,因为将来这个数组可能特别大$arr2

更新:

不便之处,抱歉,这里的完全匹配意味着 from 的值$arr1应该是$arr2ie 的 key 的子集,如果有 key2nd level building$arr2应该认为是 的匹配2nd level,但2nd levelbuilding不应该是匹配。

标签: phplaravel

解决方案


您可以循环$arr1,并检查其在键中匹配的元素,$arr2并将匹配值设置为$arr1,将空字符串设置为在键中找不到的元素$arr2

foreach($arr1 as $i => &$elem) {
    $val = getMatchedValue($arr2, $elem);
    $elem = ($val)? $val: $elem = '';
}

function getMatchedValue($hash, $elem) {
    $subs = explode(' ', $elem);
    $val = null;
    foreach($subs as $i => $sub) {
        if (isset($hash[$sub])) {
            if ($i == 0 || $val === $hash[$sub]) {
                $val = $hash[$sub];
            } else {
                return null;
            }
        } else {
            return null;
        }
        if (($i+1) == count($subs)) {
            if ($val && isset($hash[$sub]) && $val === $hash[$sub]) {
                return $val;
            } else {
                return null;
            }
        }
    }
}

推荐阅读