检查一个数组的值是否与另一个数组的键匹配-PHP

Aayush Dahal

我有两个数组:

<?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键中是否有任何值$arr22)如果值不完全匹配,我们需要从的值中拆分单词,$arr1并检查是否所有单词都匹配并且具有相同的值。

即,例如

  1. 检查是否2nd level与中的任何键匹配$arr2
  2. 如果2nd level不罚款,将任何匹配拆分2nd level2nd和的匹配项level如果2ndlevel匹配到具有相同值的键。

在这种情况下,两个2ndlevel具有价值375这一手段本应被视为匹配。如果2nd375level376,这将不是匹配项。

如果匹配:

上面我提到了两种可以视为匹配的方式,如果找到匹配,则将结果写入$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应该是key的子集,$arr2即,如果其中有key 2nd level building$arr2则应将其视为的匹配项2nd level,但是2nd levelbuilding不应该将其作为匹配项。

蔡可贺

您可以循环$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;
            }
        }
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

PHP检查数组值是否在相同位置匹配另一个数组的值

如何检查一个数组是否在另一个数组PHP中具有一个接一个的值?

PHP-检查数组值是否在另一个数组中

使用PHP,按键搜索一个数组,然后用匹配键中的值替换另一个数组中的值

PHP:如何将一个数组中的键与另一个数组中的值进行比较,并返回匹配项?

PHP检查数组是否包含另一个数组的所有数组值

数组PHP,检查另一个数组是否存在多个元素

PHP使用另一个数组中的值和键更新数组

php如何通过另一个数组值搜索数组键

如何从另一个数组的值构建PHP中多维数组的键?

如何在PHP的另一个数组中将数组存储为键的值?

PHP检查是否另一个数组的一部分

用PHP中的另一个数组键替换一个数组键

合并两个值的两个数组,并使其成为一个数组,就像php中的另一个键一样

php数组将值从一个数组添加到另一个数组

PHP数组将值从一个数组合并到另一个数组

PHP数组搜索-如何在另一个数组中找到数组键的匹配项?

PHP-如何将一个数组的元素作为键映射到另一个数组,并以数组的形式返回结果值?

PHP从另一个数组创建数组

php-将一个数组中的值与另一个数组中的值互斥

PHP在一个数组中查找作为值存在于另一个数组中的键,然后拉回该值

PHP将值从一个数组插入另一个

PHP如何将一个数组的值赋给另一个数组中的变量

如何在PHP中将一个数组值分配给另一个数组

在 PHP 中将值从一个数组移动到另一个数组

将一个数组与另一个数组进行比较,并替换缺少的值php

如何在 PHP 中将一个数组设置为另一个数组的值?

PHP将键从数组移到另一个数组

使用另一个数组键值 PHP 更改数组键