我不明白为什么我的代码无效。我想比较一个字符串与已爆炸的数据数组,以及正在解析它的数据。出于某种原因,我得到了第一个匹配,但没有剩余。我很感激这里的任何帮助!我看了看,但没有找到答案!
通过知道哪个数组元素匹配哪个字符串使我能够正确存储该数据并对其执行计算。这样做的目的是从以特定格式输入的原始调查数据创建一系列坐标。
我希望我已经解释得这么好了。就像我提到的那样,我已经找到了答案,找不到适合我正在做的事情。
感谢您的帮助,我很感激!
我尝试过使用in_array但没有成功,strcmp没有成功。
肖恩
这是我的代码:
<?php
$textArea = explode("\r", $_POST['textArea']);
$i = 0;
$j = 0;
foreach ($textArea as $textRows) {
$j = 0;
$textRow = explode(",", $textRows);
foreach ($textRow as $textItem[$i][$j]) {
if ($j == 0) {
if (("TR") == ($textItem[$i][$j])) {
echo("Traverse <br>");
}
if (("CP") == ($textItem[$i][$j])) {
echo("Control <br>");
}
echo("i = $i, j = $j, textItem = " . $textItem[$i][$j] . "<br>");
}
$j++;
}
$i++;
}
echo($textItem[9][0] . "<br>");
echo($textItem[0][0] . "<br>");
echo($textItem[0][3] . "<br>");
echo($textItem[5][6] . "<br>");
$_SESSION['textNum'] = $textItem;
?>
我的测试数据:
CP,100,576.7873,6065.6221,12.542,CP
CP,101,6966.315,2226.4001,9.897,CP
TR,100,1.735,101,1.576,1,1.735,345.9961198,90.04410841,2964.26393,PROP
TR,100,1.735,101,1.576,2,1.735,340.6589997,90.04021446,3080.322203,PROP
TR,100,1.735,101,1.576,3,1.735,351.8639518,90.02168219,5448.892284,PROP
TR,100,1.735,101,1.576,4,1.735,0,90.02469919,5316.851375,PROP
TR,100,1.735,101,1.576,5,1.735,19.26823792,90.02318399,5441.916153,PROP
TR,100,1.735,101,1.576,6,1.735,22.77896709,89.9901807,6138.435694,PROP
TR,100,1.735,101,1.576,7,1.735,5.354105397,89.98465774,7551.171809,PROP
TR,100,1.735,101,1.576,8,1.735,0,90.00156466,6884.324702,PROP
TR,100,1.735,101,1.576,9,1.735,313.0231053,90.03002234,5137.515594,PROP
TR,100,1.735,101,1.576,10,1.735,308.3168227,90.10108781,2655.989628,PROP
TR,100,1.735,101,1.576,11,1.735,340.0064751,11.68927863,757.4152317,PROP
TR,100,1.735,101,1.576,12,1.735,4.661110613,173.921569,776.3980052,PROP
TR,100,1.735,101,1.576,13,1.735,89.44844992,89.87552303,484.2277925,PROP
TR,100,1.735,101,1.576,14,1.735,169.5580845,89.57660413,273.6283687,PROP
TR,100,1.735,101,1.576,15,1.735,273.9971744,90.03246625,331.7785889,PROP
我的当前输出:
Control
i = 0, j = 0, textItem = CP
i = 1, j = 0, textItem = CP
i = 2, j = 0, textItem = TR
i = 3, j = 0, textItem = TR
i = 4, j = 0, textItem = TR
i = 5, j = 0, textItem = TR
i = 6, j = 0, textItem = TR
i = 7, j = 0, textItem = TR
i = 8, j = 0, textItem = TR
i = 9, j = 0, textItem = TR
i = 10, j = 0, textItem = TR
i = 11, j = 0, textItem = TR
i = 12, j = 0, textItem = TR
i = 13, j = 0, textItem = TR
i = 14, j = 0, textItem = TR
i = 15, j = 0, textItem = TR
i = 16, j = 0, textItem = TR
TR
CP
6065.6221
1.735
我想看到的内容:
Control
i = 0, j = 0, textItem = CP
Control
i = 1, j = 0, textItem = CP
Traverse
i = 2, j = 0, textItem = TR
Traverse
i = 3, j = 0, textItem = TR
Traverse
i = 4, j = 0, textItem = TR
Traverse
i = 5, j = 0, textItem = TR
Traverse
i = 6, j = 0, textItem = TR
Traverse
i = 7, j = 0, textItem = TR
等...
答案 0 :(得分:0)
细分$_POST
而不是"\n"
上的"\r"
数组,如下所示:
$textArea = explode("\n", $_POST['textArea']);
$i = 0;
foreach ($textArea as $textRows)
{
$j = 0;
$textRow = explode(",", $textRows);
foreach ($textRow as $textItem[$i][$j])
{
if ($j == 0)
{
if (("TR") == ($textItem[$i][$j]))
{
echo("Traverse <br>");
}
if (("CP") == ($textItem[$i][$j]))
{
echo("Control <br>");
}
echo("i = $i, j = $j, textItem = " . $textItem[$i][$j] . "<br>");
}
$j++;
}
$i++;
}
echo($textItem[9][0] . "<br>");
echo($textItem[0][0] . "<br>");
echo($textItem[0][3] . "<br>");
echo($textItem[5][6] . "<br>");
有关工作示例,请参阅:http://codepad.viper-7.com/XltceQ。
输出:
Control
i = 0, j = 0, textItem = CP
Control
i = 1, j = 0, textItem = CP
Traverse
i = 2, j = 0, textItem = TR
Traverse
i = 3, j = 0, textItem = TR
Traverse
i = 4, j = 0, textItem = TR
Traverse
i = 5, j = 0, textItem = TR
Traverse
i = 6, j = 0, textItem = TR
Traverse
i = 7, j = 0, textItem = TR
Traverse
i = 8, j = 0, textItem = TR
Traverse
i = 9, j = 0, textItem = TR
Traverse
i = 10, j = 0, textItem = TR
Traverse
i = 11, j = 0, textItem = TR
Traverse
i = 12, j = 0, textItem = TR
Traverse
i = 13, j = 0, textItem = TR
Traverse
i = 14, j = 0, textItem = TR
Traverse
i = 15, j = 0, textItem = TR
Traverse
i = 16, j = 0, textItem = TR
TR
CP
6065.6221
1.735