|
|
|
อยากนำค่าที่ได้จากการ loop 2 รอบ มาคำนวณอีกแต่ค่าไม่ออกช่วยทีค่ะ |
|
|
|
|
|
|
|
อยาก นำค่าที่ได้จากการ loop ของ 2 loop มาคำนวณอีกรอบค่ะ
อยากได้ค่าจาก $xc และ $yc ที่ ตำแหน่ง $key ที่ตรงกันอะค่ะ
นำค่าจาก $xc และ $yc มาใส้สูตรหา $xr ค่ะ
Code (PHP)
<?php
$Qdata = "test.txt" ;
$assay = file($Qdata) ;
$i=0;
$a=0;
$n=0;
while (list ($line_num, $line) = each ($assay))
{
//-----
$Xre = substr($line,0,17) ;
if($Xre > 30000 & $Xre < 75000){
$X[$i]=$Xre ;
$i++;
}
$Yre = substr($line,0,17) ;
if($Yre > 75000 & $Yre < 90000){
$Y[$a]=$Yre ;
$a++;
}
}
while(list($key, $value) = each($X)){
echo "key: $key; Value : $value <br>" ;
$xa = $value;
$xb = $X[$key+1];
$xc = $xb-$xa;
echo " --------------------------------------> $xb - $xa = $xc <br>";
}
//-----
while(list($key, $value) = each($Y)){
echo "key: $key; Value : $value <br>" ;
$ya = $value;
$yb = $Y[$key+1];
$yc = $yb-$ya;
echo " --------------------------------------> $yb - $ya = $yc <br>";
}
$xr = sqrt(pow($xc,2)+pow($yc,2));
echo " root = $xr <br>";
?>
รันแล้วได้ข้อมูลประมาณนี้ค่ะ แต่ $xr ไม่ออกค่ะ
อยากให้คำนวน $xr ตั้งแต่ key:0 ถึง key:19 อะค่ะ
key: 0; Value : 31206.44150686377
--------------------------------------> 31205.0 - 31206.44150686377 = -1.44150686377
key: 1; Value : 31205.0
--------------------------------------> 31204.26708984375 - 31205.0 = -0.73291015625
key: 2; Value : 31204.26708984375
--------------------------------------> 41203.05810546875 - 31204.26708984375 = 9998.79101562
key: 3; Value : 41203.05810546875
--------------------------------------> 41201.34423828123 - 41203.05810546875 = -1.71386718752
key: 4; Value : 41201.34423828123
--------------------------------------> 41195.0 - 41201.34423828123 = -6.34423828123
key: 5; Value : 41195.0
--------------------------------------> 41194.60400390623 - 41195.0 = -0.395996093772
key: 6; Value : 41194.60400390623
--------------------------------------> 41193.40966796873 - 41194.60400390623 = -1.1943359375
key: 7; Value : 41193.40966796873
--------------------------------------> 41191.7236328125 - 41193.40966796873 = -1.68603515623
key: 8; Value : 41191.7236328125
--------------------------------------> 41185.0 - 41191.7236328125 = -6.7236328125
key: 9; Value : 41185.0
--------------------------------------> 41184.92138671875 - 41185.0 = -0.07861328125
key: 10; Value : 41184.92138671875
--------------------------------------> 41183.74267578125 - 41184.92138671875 = -1.1787109375
key: 11; Value : 41183.74267578125
--------------------------------------> 41194.60400390623 - 41183.74267578125 = 10.861328125
key: 12; Value : 41194.60400390623
--------------------------------------> 41193.40966796873 - 41194.60400390623 = -1.1943359375
key: 13; Value : 41193.40966796873
--------------------------------------> 41191.7236328125 - 41193.40966796873 = -1.68603515623
key: 14; Value : 41191.7236328125
--------------------------------------> 41185.0 - 41191.7236328125 = -6.7236328125
key: 15; Value : 41185.0
--------------------------------------> 41194.60400390623 - 41185.0 = 9.60400390623
key: 16; Value : 41194.60400390623
--------------------------------------> 41193.40966796873 - 41194.60400390623 = -1.1943359375
key: 17; Value : 41193.40966796873
--------------------------------------> 41191.7236328125 - 41193.40966796873 = -1.68603515623
key: 18; Value : 41191.7236328125
--------------------------------------> 41185.0 - 41191.7236328125 = -6.7236328125
key: 19; Value : 41185.0
--------------------------------------> - 41185.0 = -41185
key: 0; Value : 80848.77511208203
--------------------------------------> 80848.69018554687 - 80848.77511208203 = -0.0849265351571
key: 1; Value : 80848.69018554687
--------------------------------------> 80848.68334960937 - 80848.69018554687 = -0.0068359375
key: 2; Value : 80848.68334960937
--------------------------------------> 80848.67211914062 - 80848.68334960937 = -0.01123046875
key: 3; Value : 80848.67211914062
--------------------------------------> 80848.65600585937 - 80848.67211914062 = -0.01611328125
key: 4; Value : 80848.65600585937
--------------------------------------> 80848.32128906249 - 80848.65600585937 = -0.33471679689
key: 5; Value : 80848.32128906249
--------------------------------------> 80848.31591796875 - 80848.32128906249 = -0.00537109373545
key: 6; Value : 80848.31591796875
--------------------------------------> 80848.29956054687 - 80848.31591796875 = -0.016357421875
key: 7; Value : 80848.29956054687
--------------------------------------> 80848.27636718751 - 80848.29956054687 = -0.0231933593604
key: 8; Value : 80848.27636718751
--------------------------------------> 80847.95776367187 - 80848.27636718751 = -0.31860351564
key: 9; Value : 80847.95776367187
--------------------------------------> 80847.95654296875 - 80847.95776367187 = -0.001220703125
key: 10; Value : 80847.95654296875
--------------------------------------> 80847.93798828123 - 80847.95654296875 = -0.0185546875146
key: 11; Value : 80847.93798828123
--------------------------------------> 80848.31591796875 - 80847.93798828123 = 0.377929687515
key: 12; Value : 80848.31591796875
--------------------------------------> 80848.29956054687 - 80848.31591796875 = -0.016357421875
key: 13; Value : 80848.29956054687
--------------------------------------> 80848.27636718751 - 80848.29956054687 = -0.0231933593604
key: 14; Value : 80848.27636718751
--------------------------------------> 80847.95776367187 - 80848.27636718751 = -0.31860351564
key: 15; Value : 80847.95776367187
--------------------------------------> 80848.31591796875 - 80847.95776367187 = 0.358154296875
key: 16; Value : 80848.31591796875
--------------------------------------> 80848.29956054687 - 80848.31591796875 = -0.016357421875
key: 17; Value : 80848.29956054687
--------------------------------------> 80848.27636718751 - 80848.29956054687 = -0.0231933593604
key: 18; Value : 80848.27636718751
--------------------------------------> 80847.95776367187 - 80848.27636718751 = -0.31860351564
key: 19; Value : 80847.95776367187
--------------------------------------> - 80847.95776367187 = -80847.9577637
root = 0
Tag : PHP, HTML/CSS, JavaScript
|
ประวัติการแก้ไข 2015-12-17 22:55:39
|
|
|
|
|
Date :
2015-12-17 22:54:11 |
By :
sammam |
View :
1012 |
Reply :
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เปลี่ยน loop ใหม่ เอาความสำคัญ คือ key เดียวกัน [0-19] เป็นตัวกำหนด
Code (PHP)
//while(list($key, $value) = each($X)){
// ไม่เคยใช้ รูปแบบข้างบน ชอบใช้ข้างล่าง แค่คำสั่งเดียว
// คำสั่งบน ต้องผ่าน While() List() Each()
foreach($X as $key=>$value){
echo "key: $key; Value : $value <br>" ;
$xa = $value;
$xb = $X[$key+1];
$xc = $xb-$xa;
echo " X[$key] -------> $xb - $xa = $xc <br>";
/////////////////////////////
echo "key: $key; Value : $Y[$key] <br>" ;
$ya = $Y[$key];
$yb = $Y[$key+1];
$yc = $yb-$ya;
echo " Y[$key] -------> $yb - $ya = $yc <br>";
/////////////////////////////
$xr = sqrt(pow($xc,2)+pow($yc,2));
echo " root = $xr <br>";
}
|
|
|
|
|
Date :
2015-12-18 08:16:13 |
By :
NewbiePHP |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (PHP)
<?php
//$Qdata = "test.txt" ;
//$assay = file($Qdata) ;
$assay = array(
31206.44150686377,
81205.0,
31204.26708984375,
81203.05810546875,
41201.34423828123,
81195.0,
41194.60400390623,
81193.40966796873,
41191.7236328125,
81185.0
);
$X=array();
$Y=array();
foreach ($assay as $line_num=>$line){
$re = (float)substr($line,0,17);
if($re > 30000 && $re < 75000){
$X[]=$re;
}elseif($re > 75000 && $re < 90000){
$Y[]=$re;
}
}
foreach($X as $key=>$xa){
echo " key=$key <br>";
$key1=$key + 1;
if ($key1<count($X)){
$xb = $X[$key1];
$xc = $xb - $xa;
echo " X[$key]=$X[$key], X[$key1]=$X[$key1] ---------> $xb - $xa = $xc <br>";
$ya = $Y[$key];
$yb = $Y[$key1];
$yc = $yb-$ya;
echo " Y[$key]=$Y[$key], Y[$key1]=$Y[$key1] ---------> $yb - $ya = $yc <br>";
$xp = pow($xc,2); $yp=pow($yc,2);
$xr = sqrt($xp + $yp);
echo " $xp + $yp = ".($xp + $yp)." ;;; root = $xr <br>";
}
}
result ที่ได้
Code
key=0
X[0]=31206.441506864, X[1]=31204.267089844 ---------> 31204.267089844 - 31206.441506864 = -2.17441702
Y[0]=81205, Y[1]=81203.058105469 ---------> 81203.058105469 - 81205 = -1.9418945310026
4.7280893768655 + 3.7709543695379 = 8.4990437464034 ;;; root = 2.9153119466711
key=1
X[1]=31204.267089844, X[2]=41201.344238281 ---------> 41201.344238281 - 31204.267089844 = 9997.077148437
Y[1]=81203.058105469, Y[2]=81195 ---------> 81195 - 81203.058105469 = -8.0581054689974
99941551.511801 + 64.933063749486 = 99941616.444865 ;;; root = 9997.0803960389
key=2
X[2]=41201.344238281, X[3]=41194.604003906 ---------> 41194.604003906 - 41201.344238281 = -6.740234375
Y[2]=81195, Y[3]=81193.409667969 ---------> 81193.409667969 - 81195 = -1.5903320310026
45.430759429932 + 2.5291559688329 = 47.959915398765 ;;; root = 6.9253097691558
key=3
X[3]=41194.604003906, X[4]=41191.723632812 ---------> 41191.723632812 - 41194.604003906 = -2.8803710940047
Y[3]=81193.409667969, Y[4]=81185 ---------> 81185 - 81193.409667969 = -8.4096679689974
8.2965376391776 + 70.722515348781 = 79.019052987958 ;;; root = 8.8892661670105
key=4
ปล ยังไงก็ตรวจสอบ เครื่องหมาย +/- ให้ดีด้วยนะครับ เพราะ ยกกำลัง 2 มันจะได้ค่า + เสมอ
ก็ไม่รู้ว่า เป็นค่าลบแล้ว ยกกำลังแล้วจะต้องเป็นค่าลบ อยู่อีกหรือเปล่า
|
ประวัติการแก้ไข 2015-12-18 10:45:19 2015-12-18 10:46:03 2015-12-18 10:51:40
|
|
|
|
Date :
2015-12-18 10:43:32 |
By :
NewbiePHP |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ตอบความคิดเห็นที่ : 3 เขียนโดย : NewbiePHP เมื่อวันที่ 2015-12-18 10:43:32
รายละเอียดของการตอบ ::
พี่ค่ะขอเพิ่มเติมหน่อยค่ะ
Code (PHP)
<?php
$Qdata = "test.txt" ;
$assay = file($Qdata) ;
$X=array();
$Y=array();
foreach ($assay as $line_num=>$line){
$re = (float)substr($line,0,17);
if($re > 30000 && $re < 75000){
$X[]=$re;
}elseif($re > 75000 && $re < 90000){
$Y[]=$re;
}
}
foreach($X as $key=>$xa){
echo " key=$key <br>";
$key1=$key + 1;
if ($key1<count($X)){
$xb = $X[$key1];
$xc = $xb - $xa;
echo " X[$key]=$X[$key], X[$key1]=$X[$key1] ---------> $xb - $xa = $xc <br>";
$ya = $Y[$key];
$yb = $Y[$key1];
$yc = $yb-$ya;
echo " Y[$key]=$Y[$key], Y[$key1]=$Y[$key1] ---------> $yb - $ya = $yc <br>";
$xp = pow($xc,2); $yp=pow($yc,2);
$xr = sqrt($xp + $yp);
echo " $xp + $yp = ".($xp + $yp)." ;;; root[$key] = $xr <br>";
$cp = $xr[$key];
$cq = $xr[$key+1];
$cr = $R[$key];
$Dee = acos((pow($cr,2)-pow($cp,2)-pow($cq,2))/@(2*$cp*$cq));
$Deed = rad2deg($Dee);
echo "degree = $Deed";
}
}
?>
เพิ่มเติมต้องการหา $degree ค่ะ จากสูตร
Code (PHP)
$Dee = acos((pow($cr,2)-pow($cp,2)-pow($cq,2))/@(2*$cp*$cq));
$Deed = rad2deg($Dee);
echo "degree = $Deed";
แต่ค่ารันไม่ออกค่ะ
และก็ขึ้นว่า
key=0
X[0]=31206.4415069, X[1]=31205 ---------> 31205 - 31206.4415069 = -1.44150686377
Y[0]=80848.7751121, Y[1]=80848.6901855 ---------> 80848.6901855 - 80848.7751121 = -0.0849265351571
2.0779420383 + 0.0072125163738 = 2.08515455467 ;;; root[0] = 1.44400642473
Warning: Division by zero in D:\XAMPP\htdocs\plan5.php on line 49
degree = 90 key=1
X[1]=31205, X[2]=31204.2670898 ---------> 31204.2670898 - 31205 = -0.73291015625
Y[1]=80848.6901855, Y[2]=80848.6833496 ---------> 80848.6833496 - 80848.6901855 = -0.0068359375
0.537157297134 + 4.67300415039E-5 = 0.537204027176 ;;; root[1] = 0.732942035345
Warning: Division by zero in D:\XAMPP\htdocs\plan5.php on line 49
degree = 90 key=2
X[2]=31204.2670898, X[3]=41203.0581055 ---------> 41203.0581055 - 31204.2670898 = 9998.79101562
Y[2]=80848.6833496, Y[3]=80848.6721191 ---------> 80848.6721191 - 80848.6833496 = -0.01123046875
99975821.7741 + 0.000126123428345 = 99975821.7743 ;;; root[2] = 9998.79101563
Warning: Division by zero in D:\XAMPP\htdocs\plan5.php on line 49
degree = 90 key=3
X[3]=41203.0581055, X[4]=41201.3442383 ---------> 41201.3442383 - 41203.0581055 = -1.71386718752
Y[3]=80848.6721191, Y[4]=80848.6560059 ---------> 80848.6560059 - 80848.6721191 = -0.01611328125
2.93734073646 + 0.000259637832642 = 2.9376003743 ;;; root[3] = 1.71394293204
|
|
|
|
|
Date :
2015-12-18 14:25:00 |
By :
sammam |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36.$cp = $xr[$key];
$xr มันไม่ใช่ตัวแปรชนิด array นะครับ
มาจาก บันทัดที่ 30 $xr = sqrt($xp + $yp);
ก็ดูความต้องการให้ดีครับ ต้องการ อะไรแน่ แล้วค่อยกำหนดค่า
|
|
|
|
|
Date :
2015-12-19 10:31:15 |
By :
NewbiePHP |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|