在 PHP 中获取三维数组中的值


fetching values in three dimensional array in php

我有一个PHP数组,其中在管道中,类和厚度,压力,OD,Id以三维方式合并。

现在我想匹配管道尺寸,类然后选择压力和厚度

<?php
// Pipe Size, class, OD, ID, Thickness, Pressure.
$data = array(
   “size 100”=>array(“K7” => array(112, 95, 4.5, 12.0),
            (“K8” => array(112, 95, 3.5, 11.0),
                    (“K9” => array(112, 95, 2.5, 11.0)),
   “size 150”=>array(“K7” => array(163, 145, 4.6, 10.0),
            (“K8” => array(163, 145, 3.8, 13.0),
                    (“K9” => array(163, 145, 2.9, 15.0)),
   “size 200”=>array(“K7” => array(210, 195, 5.5, 10.0),
            (“K8” => array(210, 195, 4.1, 13.0),
                    (“K9” => array(210, 195, 3.5, 15.0))
);

我想要压力和厚度,当 html 表单提交某些尺寸和类别时。结果将按以下方式发布:

When the thickness is above: ($ans1) mm, 
the Pressure would be: arraived: ($ans2) Mpa

HTML 表单是

<form method="post" name="data" action="data.php">
PIPE SIZE :  
  <select name="size" style="width: 100px"   >
  <option value="100">100 mmm 
  <option value="150"> 150 mm
  <option value="200">200 mm 
  </select>
<br><br>
 CLASS: 
<select name="class" style="width: 100px"  >
  <option value="K7"> K7
  <option value="K8"> K8
  <option value="K9"> K9
</select>
<br><br>
<INPUT TYPE="button" VALUE="SUBMIT">&nbsp;
<input type="Reset"  name="reset" value ="RESET" onClick ="(form);" />
 </form>

这就是我要做的:

假设您有一个这样的数组(您的数组需要一些编辑(

$data = array(
    "100" => array(
        "K7" => array(112, 95, 4.5, 12.0),
        "K8" => array(112, 95, 3.5, 11.0),
        "K9" => array(112, 95, 2.5, 11.0)
        ),
    "150" => array(
        "K7" => array(163, 145, 4.6, 10.0),
        "K8" => array(163, 145, 3.8, 13.0),
        "K9" => array(163, 145, 2.9, 15.0)
        ),
    "200" => array(
        "K7" => array(210, 195, 5.5, 10.0),
        "K8" => array(210, 195, 4.1, 13.0),
        "K9" => array(210, 195, 3.5, 15.0)
        )
);

然后像这样查找这些类和大小:

if ($_POST) {
    $size = $_POST['size'];
    $class = $_POST['class'];
    $thickness = $data[$size][$class][2];
    $pressure = $data[$size][$class][3];
    echo "When the thickness is above: ($thickness) mm<br>";
    echo "the Pressure would be: arraived: ($pressure) Mpa";
}

同时更改您的submit input从:<INPUT TYPE="button" VALUE="SUBMIT"到此<input type="submit" value="submit">

这应该有效:

<?php 
$size_param = $_POST("size"); // like "size 100"
$class_param = $_POST("class"); // like "K7"
$pressure = $data[$size_param][$class_param][3];
$thickness = $data[$size_param][$class_param][2];
echo "When the thickness is above: ($size_param) mm,<br>the Pressure would be: arraived: ($pressure) Mpa";
?>