我正在學習PHP,並一直在苦苦思索如何正確編寫我的代碼。我有一個接受用戶輸入的textarea,它是由空格分隔的幾行座標(PT#Northing Easting)。PHP foreach循環中的數組作用域
我有表單將textarea輸入傳遞給一個php腳本來處理它。
我可以得到一個foreach循環來完成我想要的,但只對foreach循環是局部的,變量不會傳遞到循環的外部。
我知道這是一個全局變量的作用域的問題,對我的生活我無法解決此...
下面是我的PHP代碼,我離開了HTML表單,我與它沒有任何問題並知道textarea正在正確傳遞。
**Sample data that I am inputing:
1 728258.24000 774337.29700
2 728232.15735 774277.54650
3 728326.39614 774216.82428**
<?php
$i = 0;
$j = 0;
//The code below explodes my textarea into a string array
//of separated lines.
$textArea = explode("\r", $_POST['textArea']);
$textNum = array();
//The code below works internally, but the values remain here
//I wanted to get them to the variables below so I can do work
//to them.
foreach ($textArea as $textRows) {
//The code below explode the lines into elements separated by a space
$textRow = explode(" ", $textRows);
foreach ($textRow as $textItem) {
$textNum[i][j] = $textItem;
//The code below works within a local context
echo "(" . $i . " " . $j . ")" . $textNum[i][j] . "</br>";
$j++;
}
$i++;
$j = 0;
}
//The code below is not receiving values from the foreach loop
//I know this has something to do with the variable scope
//I must be way off in my approach any help would be appreciated!
echo "</br>";
echo "</br> 0 0 " . $textNum[0][0];
echo "</br> 0 1 " . $textNum[0][1];
echo "</br> 0 2 " . $textNum[0][2];
echo "</br> 1 0 " . $textNum[1][0];
echo "</br> 1 1 " . $textNum[1][1];
echo "</br> 1 2 " . $textNum[1][2];
echo "</br> 2 0 " . $textNum[2][0];
echo "</br> 2 1 " . $textNum[2][1];
echo "</br> 2 2 " . $textNum[2][2];
echo "</br> 3 0 " . $textNum[3][0];
echo "</br> 3 1 " . $textNum[3][1];
echo "</br> 3 2 " . $textNum[3][2];
?>
我希望我已經很好地解釋了這一點,並感謝我能得到的任何幫助!謝謝!
謝謝,我知道我是紡紗我的車輪不必要的...這是一個愚蠢的錯誤.. – sean2me