我有一个创建6个数组的表单。它们是我需要多重的高度和宽度。
Array1[0] * Array2[0]
Array3[0] * Array4[0]
Array5[0] * Array6[0]
我可以单独打印数组,但我无法进行计算。
extract($_REQUEST);
if(count($_POST)!=0){
foreach($_POST as $key=>$value){
if(gettype($value) == "array"){
$i=0;
$total = array();
foreach($picw as $key=>$width){
$total[] = $width * $pich[$key];
echo $total;
}
/*foreach($value as $v){
$i++;
//echo "<div>{$key}{$i}: " . ${"{$key}{$i}"} = $v . "</div>";
${"{$key}{$i}"} = $v;
$pic{$i} = $picw{$i} * $pich{$i};
}*/
}
}
}
这是数组打印的内容:
pich=>Array pich1: 12
picw=>Array picw1: 12
hungh=>Array hungh1: 12
hungw=>Array hungw1: 12
sliderh=>Array sliderh1: 12
sliderw=>Array sliderw1: 12
编辑:这是名称来自
的表单<form action="windowQuoteEnd.php" method="post">
<table>
<td colspan="3"><b>Picture Window Quantity:</b></td>
<td><select name="picq" id="picq" class="dynamicAdder" data-selector="#pic_dup_1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select></td>
</tr>
<!-- DUPLICATES BASED ON THE SELECTION ABOVE - NUMBERS INCRIMENT FOR EACH ADDED SELECTION WHERE THE 1 IS NOW -->
<tr id="pic_dup_1">
<td><select name="pich[]" id="pich1">
<option value="12">12</option>
<option value="24">24</option>
<option value="36">36</option>
<option value="48">48</option>
<option value="60">60</option>
<option value="72">72</option>
<option value="84">84</option>
<option value="96">96</option>
<option value="108">108</option>
</select></td>
<td> x </td>
<td><select name="picw[]" id="picw1">
<option value="12">12</option>
<option value="24">24</option>
<option value="36">36</option>
<option value="48">48</option>
<option value="60">60</option>
<option value="72">72</option>
</select></td>
</tr>
<!-- END DUPLICATE -->
<tr>
<td colspan="3"><b>Single Hung Window Quantity:</b></td>
<td><select name="shq" id="shq" class="dynamicAdder" data-selector="#hung_dup_1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select></td>
</tr>
<!-- DUPLICATES BASED ON THE SELECTION ABOVE - NUMBERS INCRIMENT FOR EACH ADDED SELECTION WHERE THE 1 IS NOW -->
<tr id="hung_dup_1">
<td><select name="hungh[]" id="hungh1">
<option value="12">12</option>
<option value="24">24</option>
<option value="36">36</option>
<option value="48">48</option>
<option value="60">60</option>
<option value="72">72</option>
<option value="84">84</option>
<option value="96">96</option>
<option value="108">108</option>
<option value="120">120</option>
</select></td>
<td> x </td>
<td><select name="hungw[]" id="hungw1">
<option value="12">12</option>
<option value="24">24</option>
<option value="36">36</option>
<option value="48">48</option>
<option value="60">60</option>
<option value="72">72</option>
</select></td>
</tr>
<!-- END DUPLICATE -->
<tr>
<td colspan="3"><b>Patio Slider Quantity:</b></td>
<td><select name="psq" id="psq" class="dynamicAdder" data-selector="#test_dup_1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select></td>
</tr>
<!-- DUPLICATES BASED ON THE SELECTION ABOVE - NUMBERS INCRIMENT FOR EACH ADDED SELECTION WHERE THE 1 IS NOW -->
<tr id="test_dup_1">
<td><select name="sliderh[]" id="sliderh1">
<option value="12">12</option>
<option value="24">24</option>
<option value="36">36</option>
<option value="48">48</option>
<option value="60">60</option>
<option value="72">72</option>
<option value="84">84</option>
<option value="96">96</option>
<option value="108">108</option>
<option value="120">120</option>
</select></td>
<td> x </td>
<td><select name="sliderw[]" id="sliderw1">
<option value="12">12</option>
<option value="24">24</option>
<option value="36">36</option>
<option value="48">48</option>
<option value="60">60</option>
<option value="72">72</option>
</select></td>
</tr>
<!-- END DUPLICATE -->
<tr>
<td colspan="2"><button name="Submit" type="Submit" value="Submit">Get Quote</button></td>
</tr>
</table>
这是JQuery ......
<script>
$(document).ready(function()
{
$('.dynamicAdder').each(function(i,e)
{
var selector = $(e).attr('data-selector');
var id = $(selector).attr('id');
$(selector).addClass($(selector).attr('id')).removeAttr('id');
$(e).attr('data-selector','.'+id);
$(e).on('change',function()
{
var selector = $(this).attr('data-selector');
var qty = $(this).val();
var len = $(selector).length;
for(var x=len;x<qty;x++)
{
var clone = $(selector).last().clone();
$(selector).last().after(clone);
}
if(len>qty)
{
var diff = len-qty; // extra rows , delete them in the next step
for(var y=0;y<diff;y++)
{
$(selector).last().remove();
}
}
});
});
});
</script>
答案 0 :(得分:1)
您可以首先遍历数组并将其第0个值附加到另一个数组;然后,将值块化为2并将每个值相乘以得到结果。这假定$_POST
仅包含您想要乘以的数组,并且顺序正确:
$array_values = array();
foreach($_POST as $key=>$value){
if(gettype($value) == "array"){
$array_values[] = $value[0];
}
}
$nested = array_chunk($array_values, 2);
$results = array();
// We know that the chunked arrays have only two values
foreach ($nested as $array) {
$results = $array[0] * $array[1];
}
或者对于函数式编程爱好者:
$results = array_map(function ($array) {
return array_reduce($array, function ($prev, $next) {
return $prev * $next;
}, 1);
}, array_chunk($array_values, 2);