I don't understand why my code is not working. I want to compare a string against an array of data that has been exploded, and while it is being parsed thru. For some reason I get a match on the first one, but not the remainders. I would appreciate any help here! I have looked and looked but no answer found!
By knowing which array element matches which string enables me to store that data properly and perform calculations on it. The goal of this is to create a series of coordinates from raw survey data entered in a particular format.
I hope I have explained this well enough. Like I mentioned I have searched for an answer and can't find one suited to what I am doing.
Thanks for any help I get, I appreciate it!
I have tried using in_array with no success, strcmp no success.
Sean
Here is my code:
<?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;
?>
My Test Data:
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
My Current Output:
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
What I want to see:
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
etc...
error_reporting(E_ALL);
? You should. Add it as the first PHP line in your script. – Sven Oct 14 '12 at 22:28Compare string with array
i can only see the string .. where is the array – Baba Oct 14 '12 at 22:28in_array
– Baba Oct 14 '12 at 22:30