首先让我道歉,我是网络工程师而不是程序员..。所以如果你能忍受我的话。
这就是我所面对的,我无法为自己的一生找到一种优雅的方法。
我正在使用nagios (当然很多人都很熟悉它),并且正在从服务检查中获取性能数据。特别返回的数值如下:模块2入口温度模块2出口温度模块2 asic 4温度模块3入口温度模块3出口温度模块4入口温度模块4出口温度.等等,这些值都是在一个数组中表示的。我要做的是:匹配字符串中的前两个单词/值,以便创建数组键值的“组”,以便使用.RRD部分我不需要任何帮助,但匹配和输出我需要。
我还应该注意,这里也可能有不同的数组值,这取决于数据来自哪个设备(即,它可能显示为“开关#1传感器#1温度”),虽然我暂时不担心,但我将使用这个脚本来评估这些值,以创建它们各自的图形。
因此,在业务上,我的想法是从原始数组中创建两个数组:最初使用preg_match查找/..outlet.区.基/,因为这些是“热的”临时值,然后进一步细化,将新数组分解为仅为第二个值(int)或前两个值(模块#),以便以后进行比较。
第二次使用preg_match查找/. array。/因为这些是“冷”的温度,然后通过与前者相同的方式进一步细化这个新数组。
现在应该有两个带有key=>#或key=>module #的数组,然后使用array_intersect来查找这两个数组之间的匹配,并输出键,这样我就可以使用它们生成图形。
这有意义吗?换句话说,我只希望选择匹配模块#条目,以便在我的绘图中使用。即模块2入口,模块2出口,模块2 asic。然后重复-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)这里的任何帮助都是非常感谢的。我肯定我在这里的解释很混乱,所以希望有人同情我,帮个人.
提前谢谢。
发布于 2013-01-08 05:44:38
很难理解你的问题,但我想你是在追求这样的结果吗?
$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();一旦您拥有了$temp数组,您就可以使用foreach循环遍历每个模块和传感器,并打印出图形的值,或者只显示某些模块或某些传感器等等。
即使这不是你想要的,希望它能给你一些想法,你可以调整它来适应。
https://stackoverflow.com/questions/14208326
复制相似问题