前两个词多重匹配数组中的值,然后array_intersect


"first 2 words" multi-match for values in array then array_intersect?

首先让我道歉,我是一名网络工程师,不是程序员…所以,请你在这里耐心听我说。

这就是我要面对的问题,我无论如何也找不到一个优雅的方式来解决。

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

我还应该注意,这里可能有不同的数组值,这取决于数据来自的设备(即它可能显示为"Switch #1 Sensor #1 Temperature"),而我目前并不担心这一点,我将使用这个脚本来评估这些值在未来创建自己各自的图形。

所以,说到业务,我的想法是从原来的创建两个数组:首先使用preg_match查找/.outlet.|.asic。/因为这些是"热"温度,然后通过将新数组分解为仅为第二个值(int)或前两个值(module #)以供稍后比较来进一步细化

使用preg_match查找/.入口。/因为这些是"冷"温度,然后通过将新数组与前一个数组一样分解来进一步细化。

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

明白了吗?换句话说,我只希望选择匹配的模块#条目以在我的绘图中使用。即模块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;

你将使用这些数组来生成你的图形?

只要你在一个数组中有标签,在另一个数组中有temp值,并且在每个数组中标签和temp的顺序是相同的…那么你应该这样做:

// 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();

一旦你有了$temp数组,你可以使用foreach循环来运行每个模块和传感器,并为你的图形打印出值,或者只显示某些模块,或某些传感器等。

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