Вопрос

I am performing a calculation using the user input and associative arrays. Below is my PHP code

$class          = $_POST['class'];
$size           = $_POST['size'];
$gasket         = $_POST['gasket'];
$connection     = $_POST['connection'];

$flange150 = array( 
    array( flangesize  => 0.5,      /* Inches */
        flangeclass => 150,
        flangethick => 0.38,     /* Inches */
        boltdia     => 0.5,      /* Inches */
        totalbolt   => 4
    ),
    array( flangesize  => 0.75,     /* Inches */
            flangeclass => 150,
            flangethick => 0.44,     /* Inches */
            boltdia     => 0.5,      /* Inches */
            totalbolt   => 4
    ),

);

the user input size is stored in a variable $size and using this variable i need to select the array which has the same value as the user input (flangesize in array). Then from that array I must take the value of flangethick for my calculation. But i dont know how to perform this. Please help me in this regard.

Это было полезно?

Решение

Use foreach something like this

$flangethick = "";
foreach ($flange150 as $key =>$val) {
      if ($val['flangesize'] == $_POST['size']) { 
          $flangethick = $val['flangethick'];
      }
}
echo $flangethick;

So, this will have your value

Другие советы

I think the easiest way to do it is:

$flangethick = 0;
foreach ($flange150 as $item) {
   if ($item['flangesize'] == $size) {
       $flangethick = $item['flangethick'];
       break;
   }
}

And if you're using arrays, don't forget to use ' or " in indexes.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top