php-“文件”;前两个字;数组中值的多重匹配,然后数组_相交?

php-“文件”;前两个字;数组中值的多重匹配,然后数组_相交?,php,preg-match,array-intersect,Php,Preg Match,Array Intersect,首先让我道歉,我是一名网络工程师,不是一名编码员。。。所以请你在这里容忍我 这就是我要面对的,我一生都找不到一个优雅的方式来做到这一点 我正在使用nagios(当然你们中的许多人都熟悉它),并且正在从服务检查中获取性能数据。这个函数特别返回如下值: 模块2入口温度 模块2出口温度 模块2 asic-4温度 模块3入口温度 模块3出口温度 模块4入口温度 模块4出口温度 ... 等等 这些值都显示在单个数组中。我想做的是: 匹配字符串中的前2个单词/值,以便创建数组键值的“组”,用于生成具有。。。

首先让我道歉,我是一名网络工程师,不是一名编码员。。。所以请你在这里容忍我

这就是我要面对的,我一生都找不到一个优雅的方式来做到这一点

我正在使用nagios(当然你们中的许多人都熟悉它),并且正在从服务检查中获取性能数据。这个函数特别返回如下值: 模块2入口温度 模块2出口温度 模块2 asic-4温度 模块3入口温度 模块3出口温度 模块4入口温度 模块4出口温度 ... 等等 这些值都显示在单个数组中。我想做的是: 匹配字符串中的前2个单词/值,以便创建数组键值的“组”,用于生成具有。。。RRD部分我不需要任何帮助,但匹配和输出我需要

我还应该注意,根据数据来自的设备,这里可能也有不同的数组值(即,它可能显示为“开关#1传感器#1温度”),虽然我暂时不担心这一点,我将在将来使用这个脚本来评估这些值,以创建它们各自的图形

因此,说到业务,我的想法是从原始阵列创建两个阵列: 最初使用preg_match查找/.outlet.|.asic./因为它们是“热”temp,然后通过将新数组分解为第二个值(int)或前两个值(module#)进行进一步优化,以便稍后进行比较

第二步使用preg_match查找/.inflate/,因为它们是“冷”temp,然后通过将新数组分解为与前一个相同的数组来进一步细化

现在应该有两个带有key=>#或key=>module的数组# 然后使用array_intersect在两个数组中查找匹配项并输出键,以便我可以使用它们生成图形

这有意义吗?换句话说,我只希望选择匹配的模块条目,以便在绘图中使用。i、 模块2入口,模块2出口,模块2专用集成电路。。。然后对模块3入口、模块3出口等重复上述步骤

这是我尝试过的,但它根本没有按我想要的方式工作:

$test = array("module 1 inlet temperature", "module 2 inlet temperature", "module 2 asic-4 temperature", "module 2 outlet temperature", "module 1 outlet temperature");
$results = array();
foreach($test as $key => $value) {
   preg_match("/.*inlet.*|.*asic.*/", $test[$key]);
   preg_match("/module [0-9]?[0-9]/", $test[$key]);      
   $results[] = $value;
   }

if(preg_match("/.*outlet.*/", $test[$key]));
   foreach($test as $key1 => $value1) {
      preg_match("/module [0-9]?[0-9]/", $test[$key1]);
   $results1[] = $value1;
   }#
}
$results3 = array_intersect($results, $results1)
我们非常感谢您的帮助。我相信我在这里的解释是相当混乱的,所以希望有人同情我,帮助一个人


提前谢谢。

你的问题有点难理解,但我想你在追求这样的结果

$temps['module 1']['inlet'] = 20;
$temps['module 1']['outlet'] = 30;

$temps['module 2']['inlet'] = 25;
$temps['module 2']['outlet'] = 35;
$temps['module 2']['asic-4'] = 50;
然后您将使用这些数组生成图形

只要一个数组中有标签,另一个数组中有临时值,并且每个数组中标签和临时值的顺序相同。。。那么这就是你要做的:

// Split Names into Groups
$temps = array(20,25,50,35,30);
$labels = array("module 1 inlet temperature", "module 2 inlet temperature", "module 2 asic-4 temperature", "module 2 outlet temperature", "module 1 outlet temperature");

// Combine Lables to Values (Labels and Values must be in the same positions)
$data = array_combine($labels, $temps);

$temps = array();
foreach ($data as $label => $temp) {
    $words = preg_split('/\s/i', $label);

    // Combine first two pieces of label for component name
    $component = $words[0] . ' ' . $words[1];

    // Sensor name is on it's own
    $sensor = $words[2];

    // Save Results
    $temps[$component][$sensor] = $temp;
}

// Print out results for debug purposes
echo '<pre>';
var_dump($temps);
echo '</pre>';
exit();
//将名称拆分为组
$temps=阵列(20,25,50,35,30);
$labels=阵列(“模块1入口温度”、“模块2入口温度”、“模块2 asic-4温度”、“模块2出口温度”、“模块1出口温度”);
//将标签与值组合(标签和值必须位于相同位置)
$data=array\u combine($labels,$temp);
$temps=array();
foreach($label=>$temp的数据){
$words=preg_split('/\s/i',$label);
//组合元件名称的前两个标签
$component=$words[0].'.$words[1];
//传感器名称由它自己决定
$sensor=$words[2];
//保存结果
$temps[$component][$sensor]=$temp;
}
//出于调试目的打印结果
回声';
var_dump($temps);
回声';
退出();
拥有
$temp
数组后,可以使用
foreach
循环遍历每个模块和传感器,并打印出图形的值,或者仅显示某些模块或某些传感器等


即使它不是你想要的,希望它能给你一些想法,你可以调整它以适应。

太棒了。。。非常感谢。做了我想做的事!现在创建foreach循环,让它做我想做的事情!