Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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...

share|improve this question

closed as too localized by hakre, SomeKittens, tereško, ЯegDwight, t0mm13b Oct 15 '12 at 0:37

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. If this question can be reworded to fit the rules in the help center, please edit the question.

    
Can you also provide the array you are matching it against and what exactly do you want to match –  Baba Oct 14 '12 at 22:22
    
Thats in my code. I don't get why this doesn't work: ("TR") == ($textItem[$i][$j]) or ("CP") == ($textItem[$i][$j]) –  sean2me Oct 14 '12 at 22:27
    
Uninitialized $i should emit a notice. Do you run with error_reporting(E_ALL);? You should. Add it as the first PHP line in your script. –  Sven Oct 14 '12 at 22:28
    
They are 2 parts ... you said Compare string with array i can only see the string .. where is the array –  Baba Oct 14 '12 at 22:28
    
And again .. in your code there is no place you are using in_array –  Baba Oct 14 '12 at 22:30

1 Answer 1

up vote 0 down vote accepted

Breakdown the $_POST array on "\n" instead of "\r", like this:

$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>");


See: http://codepad.viper-7.com/XltceQ for a working example.


Output:

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
share|improve this answer
    
It is initialized but didn't copy over for some peculiar reason. Thats not the problem I am having though. My array is coming in just fine. From my test input when the first element of a row equals CP it contains 6 elements. When it contains TR there are 11 elements. My question really is how do I compare that first element to figure out if it is a TR or CP? In my code is $textItem[$i][$j], a string or is it still an array? Could there be a problem with spaces being entered around $textItem[$i][$j] and that causing an inequality to occur? –  sean2me Oct 14 '12 at 22:47
    
In my mind its simple I can get $textItem[$i][$j] to show it equalling TR or CP why then doesn't it simply match to TR or CP when I ask if it is equal?? –  sean2me Oct 14 '12 at 22:48
    
Take a look at the working example that I posted. It results in the anticipated output. Aside from formatting your test data as an array of strings (which might also me the issue - you didn't offer how that test data was being presented), the only change to your code that I made was the initialization of $i as I mentioned. And... it works! :) –  coderabbi Oct 14 '12 at 22:50
    
My code was complete when I looked at it when editing but because I didn't hit enter and keep a space after my "Here is my code:" it snipped a bunch of code out... –  sean2me Oct 14 '12 at 22:53
    
Perhaps. In that case, it's the formatting of the test data that is the issue, as the example at codepad.viper-7.com/09h05u works as intended. –  coderabbi Oct 14 '12 at 22:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.