在 php 中使用不同的变量获得完美匹配数组


Get perfect match array using different variables in php

我有三个变量

$region ='APJ'; 
$sub_region = 'India';
$country = 'India' 

我有多个数组,例如

Array
(
  [0] => Array
    (
        [region] => APJ
        [subregion] => India
        [country] => 
    )
  [1] => Array
    (
        [region] => APJ
        [subregion] =>china 
        [country] => 
    )
  [2] => Array
    (
        [region] => WW
        [subregion] =>france 
        [country] => France
    )
  [3] => Array
    (
        [region] => EMEA
        [subregion] =>mema 
        [country] => France
    )
)

我需要找到使用这三个变量的完美匹配数组。

在上面的例子中,我们有 4 个具有不同值的数组。

第一个数组匹配区域和子区域级别也。

第二个阵列仅区域级别匹配。

所有其他值甚至都不匹配。

所以我选择第一个数组作为结果。

那么使用 php 我如何在这种情况下找到完美的匹配数组?

概念:"排名"

  • 创建与定义数组结构相同的比较数组
  • 遍历所有定义并...
    • 每个定义的计数匹配项(排名)(循环比较数组)。
    • 将数学级别(秩)存储在定义数组本身中(附加键:_level)。
  • 保留排名最高的定义数组的索引

结果:排名最高的定义数组的索引$highestlevel).
(= 排名最高的第一个定义)

法典

<?php 
$defs = array (                              // Definitions
    array(
        'region' => 'APJ',          // to be compared
        'country' => '',            // to be compared
        'typeofrec' => 1, 
        'TotalAllocations' => 0,
        'TotalTransfersOut' => 0, 
        'subregion' => 'India',     // to be compared
        'TotalTransfersIn' => 0, 
        'StartOfAllocation' => 0, 
        'ApprovedActivities' => 10,
        'AvailableBalance' => -10, 
        'Exposure' => 0, 
    ),
    array(
        'region' => 'APJ',
        'subregion' => 'china ',
        'country' => '',
    ),
    array(
        'region' => 'WW',
        'subregion' => 'france',
        'country' => 'France',
    ),
    array(
        'region' => 'EMEA',
        'subregion' => 'mema',
        'country' => 'France',
    ),
);
// Comparison array
// Instead of ...
//   $region ='APJ'; 
//   $sub_region = 'India';
//   $country = 'India' 
$testarray = array(                          // Comparison array
    'region' => 'APJ',
    'subregion' => 'India',
    'country' => 'India',
);
// --------------------------------------------------------------------------
$highestlevel = 0;                           // Index of highest ranked definition
foreach ($defs as $ix => $def) {             // Loop through all definitions
    $defs[$ix]['_level'] = 0;                // Add new entry to array
    foreach($testarray as $key => $value) {  // Loop throug comparison values
                                             // Increment ranking level if matched
        if (trim($def[$key]) == trim($value)) $defs[$ix]['_level']++;
        // Variant "case ignored":
        // if (strtolower(trim($def[$key])) == strtolower(trim($value))) $defs[$ix]['_level']++;
    }
                                             // Keep index of highest ranked
    if (!isset($defs[$highestlevel]['_level']) || $defs[$highestlevel]['_level'] < $defs[$ix]['_level']) $highestlevel = $ix;
}
// --------------------------------------------------------------------------
                                             // Display the result
echo '<h3>Result</h3><pre>'; var_dump($defs[$highestlevel]); echo '</pre>';

主要优点:

  • 灵活性:
    代码仅假定数组结构。既不假定键名、元素类型,也不假定固定数组长度。因此,它适用于不同的值和数组长度。
  • 压 实 度。

结果

array(12) {
    ["region"]=> string(3) "APJ"
    ["country"]=> string(0) ""
    ["typeofrec"]=> int(1)
    ["TotalAllocations"]=> int(0)
    ["TotalTransfersOut"]=> int(0)
    ["subregion"]=> string(5) "India"
    ["TotalTransfersIn"]=> int(0)
    ["StartOfAllocation"]=> int(0)
    ["ApprovedActivities"]=> int(10)
    ["AvailableBalance"]=> int(-10)
    ["Exposure"]=> int(0)
    ["_level"]=> int(2)
}

请尝试这个

$region ='APJ'; 
$sub_region = 'India';
$country = 'India' 
$ret ="";
foreach($yourarr as $key=> $subArr){
    /* If region,subregion & country values matches given data return mached array value  */
    if($subArr['region'] == $region && $subArr['subregion'] == $sub_region  && $subArr['country'] == $country){
        $matArr = $arr[$key];
        break;
    }
    /* Else If any of the 2 matches with given data return the specific array value  */
    else if(
        ($subArr['region'] == $region && $subArr['subregion']) ||
        ($subArr['subregion'] == $sub_region  && $subArr['country'] == $country ) ||
        ($subArr['region'] == $region && $subArr['country'] == $country )
        ){
        $matArr = $arr[$key];
        break;
    }
    /* Else any of the value matches return the specific array value*/
    else if($subArr['region'] == $region || $subArr['subregion'] == $sub_region  || $subArr['country'] == $country){
        $matArr = $arr[$key];
        break;
    }

}
echo "Matching Array Is:";
print_r($matArr);
for($i=0;$i<count($arr);$i++)
{
    if(($arr[$i]['region']==$region) &&  ($arr[$i]['subregion']==$sub_region) &&  ($arr[$i]['country']==$country) )
    { 
        echo "3"."index".$i;
    }
    elseif( (($arr[$i]['region']==$region) &&  ($arr[$i]['subregion']==$sub_region)) || ( ($arr[$i]['subregion']==$sub_region) && ($arr[$i]['country']==$country) ) || ( ($arr[$i]['region']==$region) && ($arr[$i]['country']==$country) )  )
    {
        echo "2"."index".$i;
    }
    elseif(($arr[$i]['region']==$region) ||  ($arr[$i]['subregion']==$sub_region) || ($arr[$i]['country']==$country))
    {
       echo "1"."index".$i;
    }
    else
    { 
        echo "not found";
    }
}

$arr is your array.
in above code it first search for perfect match means all case are matche
in second case it match for two
in third case it match for atleast one
3 is the top priority and get the index value