大家好我通过使用foreach循环获取数组现在如何定义数组中不同元素的索引以进行进一步计算,就像我希望数组1的element1被数组2的element1除 每个循环的代码
<?php
if (isset($_POST['submit'])) {
$data_t1 = $_POST['t1'];
foreach ($data_t1 as $key => $value) {
echo 'T1: ' . $value . '<br />';
echo 'T2: ' . $_POST['t2'][$key] . '<br />';
echo 'T3: ' . $_POST['a1'][$key] . '<br />';
echo 'Username: ' . $_POST['username'][$key] . '<br /><br />';
}
$data_t2 = $_POST['t2'];
$data_t3=$_POST['a1'];
$data_t4=$_POST['username'];
}
var_dump($data_t1);
var_dump($data_t2);
var_dump($data_t3);
var_dump($data_t4);
?>
现在我希望这些数组值用于进一步计算
echo "Measuring Efficiency ";
echo "<table border='1' align='center'>
<tr>
<th>Inputs</th>
<th>A</th>
<th>B</th>
<th>C</th>
</tr>";
echo "<tr>";
foreach($data_t4 as $t4)
{
echo $t4; this display the values of array 4
}
foreach($data_t1 as $t1)
{
echo $t1;
$r=round(($t1/$t4),2);
$fr=round(($t1/$t4),2);
$br=round(($t1/$t4),2);
echo "<td>" ."Room Size/Consumption". "</td>";
echo "<td>".$r. "</td>";
echo "<td>".$fr. "</td>";
echo "<td>".$br. "</td>";
echo "</tr>";
echo "<tr>";
}
?
如何定义数组索引以分割两个数组的元素
我的结果是
$ data_t1 [12 12] $ data_t4 [1.44 4.32] $ data_t2 [2 4] $ data_t3 [2 3]
我想分12 / 1.44,2 / 1.44 2 / 1.44 n 12 / 4.32 4 / 4.32 3 / 4.32
我该怎么办
答案 0 :(得分:0)
这是你的意思吗?
$array_1 = array(10,30,50,70,90);
$array_2 = array(1,3,5,7,9);
foreach($array_1 as $i => $v){
$array_1[$i] = $array_1[$i]/$array_2[$i];
}
print_r($array_1);
Array
(
[0] => 10
[1] => 10
[2] => 10
[3] => 10
[4] => 10
)
或者你想用键做类似的事情,而不是像这样的值。
$array_1 = array(
10=>234234,
30=>234,
50=>23443,
70=>234,
90=>234
);
$array_2 = array(
1=>32423,
3=>4232,
5=>2342,
7=>234,
9=>234
);
$array_a = array_keys($array_1);
$array_b = array_keys($array_2);
foreach($array_a as $i => $v){
$tmp[$i] = $array_a[$i]/$array_a[$i];
}
print_r($tmp);
Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 1
[4] => 1
)
好的,根据你的编辑,我做了一些新的东西。
你说: 我想分裂 12 / 1.44 2 / 1.44 2 / 1.4412 / 4.32 4 / 4.32 3 / 4.32
$data_t4 = array(1.44,4.32);
$data_t1 = array(12,12);
$data_t2 = array(2,4);
$data_t3 = array(2,3);
$nums = array(
't1'=>$data_t1,
't2'=>$data_t2,
't3'=>$data_t3,
);
$divs = $data_t4;
foreach($nums as $i => $v){
foreach($v as $ix => $vx){
foreach($divs as $dx => $dv){
$data[$i]['n:'.$vx]['d:'.$dv] = $vx/$dv;
}
}
}
print_r($data);
Array
(
[t1] => Array
(
[n:12] => Array
(
[d:1.44] => 8.3333333333333
[d:4.32] => 2.7777777777778
)
)
[t2] => Array
(
[n:2] => Array
(
[d:1.44] => 1.3888888888889
[d:4.32] => 0.46296296296296
)
[n:4] => Array
(
[d:1.44] => 2.7777777777778
[d:4.32] => 0.92592592592593
)
)
[t3] => Array
(
[n:2] => Array
(
[d:1.44] => 1.3888888888889
[d:4.32] => 0.46296296296296
)
[n:3] => Array
(
[d:1.44] => 2.0833333333333
[d:4.32] => 0.69444444444444
)
)
)