gpt4 book ai didi

php - PHP foreach 循环中的数组范围

转载 作者:搜寻专家 更新时间:2023-10-31 21:14:50 24 4
gpt4 key购买 nike

我正在学习 PHP,并且一直在为如何正确编写代码而苦恼。我有一个接受用户输入的文本区域,它是几行由空格分隔的坐标 (PT# Northing Easting)。

我让表单将文本区域输入传递给 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];
?>

我希望我已经解释得足够好,感谢我能得到的任何帮助!谢谢!

最佳答案

你的第二个循环有一个错误:

$textNum[i][j] = $textItem;

必须是:

$textNum[$i][$j] = $textItem;

关于php - PHP foreach 循环中的数组范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11299777/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com