我有三个变量
$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如何在这种情况下找到完美的匹配数组?
答案 0 :(得分:1)
结果:排名最高的定义数组的索引( $ 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)
}
答案 1 :(得分:0)
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
答案 2 :(得分:0)
请试试这个
$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);