php – “前2个字”多个匹配数组中的值然后array_intersect?
首先让我道歉,我是一名网络工程师,而不是编码员…所以,如果你愿意在这里承担我的话请.
这就是我所反对的,我无法为我的生活找到一种优雅的方式来做到这一点. 我正在使用nagios(肯定你们很多人都熟悉它)并从服务检查中获取性能数据.这个特别返回如下值: 我还应该注意,这里可能有不同的数组值,具体取决于数据来自的设备(即它可能显示为“Switch#1 Sensor#1 Temperature”),而我并不担心这一点目前,我将使用此脚本来评估未来的这些值,以创建各自的图形. 所以,对于业务,我的想法是从原始创建两个数组: 然后使用preg_match来查找/.inlet./,因为它们是“冷”临时值,然后通过将新数组与前者相同来进一步细化. 现在应该有两个数组,其中key =>#或key =>模块# 那有意义吗?换句话说,我只希望选择匹配的模块#条目以在我的图形中使用.即模块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) 这里的任何帮助将非常感激.我相信我的解释非常令人困惑,所以希望有人对我表示同情并给予一个人一个人…… 提前致谢. 解决方法
理解你的问题有点难,但我想你是在追求这样的结果?
$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 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循环遍历每个模块和传感器并打印出图形的值,或仅显示某些模块或某些传感器等. 即使它不是你所追求的,也希望它会给你一些想法,你可以调整它以适应. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |