我正在编写一个简单的PHP页面来报告各种传感器的结果。由于传感器的数量及其位置会有所不同,我需要在我的代码中考虑到这一点。
$Location_Array = array('Sanding', 'Outside');
foreach($Location_Array as $Locations) {
${"Time_Array_" . $Locations} = array();//Array data fed via array_push via mysql query- clipped
}
$diff = array_diff(${"Time_Array_" . $Location_Array[0]}, ${"Time_Array_" . $Location_Array[1]});//This is the problem line - It does work
如您所见,我的Time_Array_LOCATION是一个通过一系列位置创建的数组。最终用户将通过设置文件设置位置。代码确实有效,但每个位置都必须手动放入array_diff。传感器在特定时间报告,如果未能报告,则所有阵列都将删除该键/值。我需要让它从Location_Array中动态地比较所有数组。
像:
$diff = array_diff($Time_Array_Sanding, $Time_Array_Outside, $Time_Array_ANOTHER);
我认为它很简单,我只是忽略它。我想循环它,但返回的值不是数组变量名等... 我希望这是有道理的。
提前感谢您的帮助。
其他信息:
这些数组只不过是MySQL时间戳。时代必须匹配。数据是(09-20-2016 9:00,09-20-2016 9:30),依此类推。如果没有9:00时间戳。我将所有数组中的数据丢弃。
$date = $row["date_time"];
$dt = substr($date, 5, 11);
array_push(${"Time_Array_" . $Locations}, $dt);
答案 0 :(得分:0)
我建议将数据存储在一个超级数组中,而不是存储在不同的变量中。它允许您通过call_user_func_array
:
$Time_Arrays = []; // Will be a 2D array -- the "super-array".
// You would fill up this data structure from the database, with something like:
/*
while ($row = $pdo->fetch()) {
// Assuming there is:
// - a column "type" which can have values "Sanding", "Outside", ...
// - a column "time_stamp" which has the date/time value to store:
$Time_Arrays[$row["type"]][] = $row['time_stamp'];
}
*/
// Since I don't have the database, here are some sample data.
// Note that only 2 date/time values are common to all:
$Time_Arrays["Sanding"] = ['09-20-2016 9:00', '09-20-2016 9:30', '09-20-2016 10:00', '09-20-2016 10:30'];
$Time_Arrays["Outside"] = ['09-20-2016 9:00', '09-20-2016 9:30', '09-20-2016 13:00', '09-20-2016 10:30'];
$Time_Arrays["Another"] = ['09-20-2016 9:00', '09-20-2016 11:30', '09-20-2016 10:00', '09-20-2016 10:30'];
// The trick with `call_user_func_array`: merge all sub-arrays together into one array,
// and then remove duplicates from it:
$merged = array_unique(call_user_func_array('array_merge', $Time_Arrays));
// Same trick to get intersection (only values that occur in all sub-arrays):
$intersected = call_user_func_array('array_intersect', $Time_Arrays);
// Finally get the overall difference (values that do not occur in all sub-arrays):
$diff = array_diff($merged, $intersected);
// Show the results
echo "Merged (all values):\n";
print_r($merged);
echo "Intersected (common values only):\n";
print_r($intersected);
echo "Diff (non-common values only):\n";
print_r($diff);
此脚本的输出为:
Merged (all values):
Array
(
[0] => 09-20-2016 9:00
[1] => 09-20-2016 9:30
[2] => 09-20-2016 10:00
[3] => 09-20-2016 10:30
[6] => 09-20-2016 13:00
[9] => 09-20-2016 11:30
)
Intersected (common values only):
Array
(
[0] => 09-20-2016 9:00
[3] => 09-20-2016 10:30
)
Diff (non-common values only):
Array
(
[1] => 09-20-2016 9:30
[2] => 09-20-2016 10:00
[6] => 09-20-2016 13:00
[9] => 09-20-2016 11:30
)
在eval.in上看到它。
一旦了解了call_user_func_array
的工作方式,就可以将其作为捷径:
$diff = call_user_func_array('array_diff', $Time_Arrays);
但是这不会产生预期的结果,因为它只返回第一个子数组中不在其他任何子数组中的值,而您还需要非值的值在第一个中,但确实发生在其他一个子数组中。上面的代码通过首先获得所有子数组的合并来克服这个问题。