我有一个如下所示的数组:
$values = array(
array("temperature",5,6,7,8,9,10),
array("-20",0.05,0.06,0.08,0.09,0.1,0.11),
array("-19",0.06,0.07,0.08,0.09,0.11,0.12),
array("-18",0.06,0.08,0.09,0.1,0.11,0.13),
array("-17",0.07,0.08,0.1,0.11,0.12,0.14),
array("-16",0.07,0.09,0.1,0.12,0.13,0.15),
array("-15",0.08,0.1,0.11,0.13,0.14,0.16)
);
我从用户输入表单中获得了两个变量
$temperature= $_POST['temperature'];
$humidity= $_POST['humidity'];
我需要帮助来遍历数组以检索与这两个变量相对应的值,并将其存储在另一个变量中,以便进行简单的计算。
例如:如果$ temperature = -20,$ humidity = 5 我需要的价值是
$values[1][$humidity-4] (which is basically 1,1)
这将是0.05。
以下是我之前使用CSV尝试的内容,但它在网页上创建了超时。
<?php
/*
Plugin Name: watercalc
Plugin URI:
Description: Calculates water generation
Version: 1.0
Author: Chefattack
Author URI:
*/
//WordPress Hooks
add_shortcode('wcalc', 'run_bc');
function run_bc()
{
// If the submit button has been pressed
if(isset($_POST['submit']))
{
// Check number values
if(is_numeric($_POST['temperature']) &&
is_numeric($_POST['humidity']))
{
// Calculate total
function getValueFromCSV($csv_name) {
$humidity= $_POST['humidity'];
$temperature= $_POST['temperature'];
$file = fopen($csv_name, "r");
while (!feof($file)) {
$arr = fgetcsv($file);
if ($arr[0] == $temperature) {
$coefficient= $arr[$humidity - 4];
if($_POST['models'] == 'pod')
{
$total = round($coefficient * 0.0287801747544059 * 0.03 *60 * 24 *365);
}
if($_POST['models'] == '36K')
{
$total = round($coefficient * 0.0287801747544059 * 0.1 *60 * 24 *365);
}
if($_POST['models'] == '55K')
{
$total = round($coefficient * 0.0287801747544059 * 0.15 *60 * 24 *365);
}
if($_POST['models'] == '110K')
{
$total = round($coefficient * 0.0287801747544059 * 0.25 *60 * 24 *365);
}
if($_POST['models'] == '180K')
{
$total = round($coefficient * 0.0287801747544059 * 0.5 *60 * 24 *365);
}
if($_POST['models'] == '365K')
{
$total = round($coefficient * 0.0287801747544059 * 1 *60 * 24 *365);
}
// Print total to the browser
echo "<h4> Water Generation at $humidity% Relative Humidity and $temperature C<br>Per Year: ~ {$total} ltrs.</h4>";
}
}
fclose($file);
}
// Function Calling
getValueFromCSV('akvo-values.csv');
} else {
// Print error message to the browser
echo 'Numeric values are required';
}
}
?>
<form method='post' action='<?php echo get_permalink(); ?>'>
<label>Temperature (Celcius)</label><br>
<select name="temperature" class="form-control" style="width: 150px; display: block">
<?php for ($i = -20; $i <= 60; $i++) : ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php endfor; ?>
</select><br>
<label>Relative Humidity (%)</label><br>
<select name="humidity" class="form-control" style="width: 150px; display: block">
<?php for ($i = 5; $i <= 100; $i++) : ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php endfor; ?>
</select><br>
<label>Choose Akvo Model</label><br>
<select name="models" class="form-control" style="width: 150px; display: block">
<option value="pod">Akvo Pod</option>
<option value="36K">Akvo 36K</option>
<option value="55K">Akvo 55K</option>
<option value="110K">Akvo 110K</option>
<option value="180K">180K</option>
<option value="365K">365K</option>
</select><br>
<input name="submit" type="submit" value="Calculate" class="btn btn-primary" />
</form>
<?php
}
答案 0 :(得分:0)
一种解决方案是从原始数组计算所需的索引
<?php
$values = array(
array("temperature",5,6,7,8,9,10),
array("-20",0.05,0.06,0.08,0.09,0.1,0.11),
array("-19",0.06,0.07,0.08,0.09,0.11,0.12),
array("-18",0.06,0.08,0.09,0.1,0.11,0.13),
array("-17",0.07,0.08,0.1,0.11,0.12,0.14),
array("-16",0.07,0.09,0.1,0.12,0.13,0.15),
array("-15",0.08,0.1,0.11,0.13,0.14,0.16)
);
function getValue($values, $temperature, $humidity) {
$temperatureIndex = (int)(21 + $temperature);
$humidityIndex = (int)($humidity - 4);
return $values[$temperatureIndex][$humidityIndex];
}
echo getValue($values, -20, 5) . PHP_EOL;
echo getValue($values, -16, 9) . PHP_EOL;
这是输出:
0.05
0.13
对于另一种解决方案,您可以使用第一个温度指数和第二个湿度指数构建一个二维查找表。
这是我的示例代码:
<?php
$values = array(
array("temperature",5,6,7,8,9,10),
array("-20",0.05,0.06,0.08,0.09,0.1,0.11),
array("-19",0.06,0.07,0.08,0.09,0.11,0.12),
array("-18",0.06,0.08,0.09,0.1,0.11,0.13),
array("-17",0.07,0.08,0.1,0.11,0.12,0.14),
array("-16",0.07,0.09,0.1,0.12,0.13,0.15),
array("-15",0.08,0.1,0.11,0.13,0.14,0.16)
);
// get first row of humidity value
$humidityValue = array_shift($values);
array_shift($humidityValue); // drop the element 'temperature'
// build lookup
$lookup = array();
foreach ($values as $value) {
// get temperature from the first element of row
$temperature = array_shift($value);
// bind himidityValue and value as associative array
$humidityOfTemperature = array_combine($humidityValue, $value);
// insert to look up
$lookup[$temperature] = $humidityOfTemperature;
}
function getValue($lookup, $temperature, $humidity) {
// if value of pair (temperature, humidity) is not defined, throw exception
if (!isset($lookup[$temperature]) || !isset($lookup[$temperature][$humidity])) {
throw new \Exception("undefined value. temperature => $temperature, humidity => $humidity");
}
return $lookup[$temperature][$humidity];
}
$temperature = -20;
$humidity = 5;
echo getValue($lookup, $temperature, $humidity) . PHP_EOL;
$temperature = -15;
$humidity = 10;
echo getValue($lookup, $temperature, $humidity) . PHP_EOL;
这是查找数组:
Array
(
[-20] => Array
(
[5] => 0.05
[6] => 0.06
[7] => 0.08
[8] => 0.09
[9] => 0.1
[10] => 0.11
)
[-19] => Array
(
[5] => 0.06
[6] => 0.07
[7] => 0.08
[8] => 0.09
[9] => 0.11
[10] => 0.12
)
[-18] => Array
(
[5] => 0.06
[6] => 0.08
[7] => 0.09
[8] => 0.1
[9] => 0.11
[10] => 0.13
)
[-17] => Array
(
[5] => 0.07
[6] => 0.08
[7] => 0.1
[8] => 0.11
[9] => 0.12
[10] => 0.14
)
[-16] => Array
(
[5] => 0.07
[6] => 0.09
[7] => 0.1
[8] => 0.12
[9] => 0.13
[10] => 0.15
)
[-15] => Array
(
[5] => 0.08
[6] => 0.1
[7] => 0.11
[8] => 0.13
[9] => 0.14
[10] => 0.16
)
)
这是输出:
0.05
0.16