【问题标题】:php - "first 2 words" multi-match for values in array then array_intersect?php - 数组中的值的“前两个单词”多重匹配然后array_intersect?
【发布时间】:2013-01-08 04:41:48
【问题描述】:

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

这就是我要面对的问题,我一辈子都找不到一种优雅的方式来做到这一点。

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

我还应该注意,这里也可能有不同的数组值,具体取决于数据来自的设备(即它可能显示为“Switch #1 Sensor #1 Temperature”),而我不是暂时担心这一点,我将在未来使用这个脚本来评估这些值以创建自己的图表。

所以,说实话,我的想法是从原始数组创建两个数组: 最初使用 preg_match 来查找 /.outlet.|.asic./ 因为这些是“热”临时文件,然后通过将新数组分解为仅第二个值(int)或前两个值(模块号)供以后比较

第二次使用 preg_match 查找 /.inlet./ 因为这些是“冷”温度,然后通过与前者相同的方式分解新数组来进一步细化。

现在应该有两个带有 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)

这里的任何帮助将不胜感激。我敢肯定我在这里的解释很混乱,所以希望有人同情我并帮助一个人......

提前致谢。

【问题讨论】:

    标签: php preg-match array-intersect


    【解决方案1】:

    您的问题有点难以理解,但我想您是在追求这样的结果?

    $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 循环遍历每个模块和传感器并打印出图表的值,或者只显示某些模块或某些传感器等。

    即使它不完全是您所追求的,希望它能给您一些想法,您可以对其进行调整以适应它。

    【讨论】:

    • 太棒了...谢谢!正是我需要的!现在创建 foreach 循环让它做我想做的事!
    猜你喜欢
    • 1970-01-01
    • 2021-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-23
    • 1970-01-01
    • 2014-05-02
    • 1970-01-01
    相关资源
    最近更新 更多