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 am trying to see if an array contains a string, but am having a ton of problems - no matter what I do it always returns false.

Basically I want to see if a stop on the train matches a particular string. I am trying to see if at any point NAME = Ridgewood.

Below is my code that I am using since it is a multi-dimensional array. Array is below code. I've tried === in_array and even strcmp and still get FALSE every time.

foreach ($stationResults as $item) {
    $stationStops = $item[STOPS][STOP];

    foreach ($stationStops as $stopitem) {
        $stationName = $stopitem[NAME];
        echo $stationName;
        echo "<br />";
        if ($stationName === "Ridgewood") {
            $stationExists = TRUE;
        }
        else $stationExists = FALSE;
    }

    var_dump($stationExists);
}

Here is the array I am searching:

[0]=>
  array(18) {
    ["ITEM_INDEX"]=>
    string(1) "0"
    ["SCHED_DEP_DATE"]=>
    string(19) "12:35:00 07/18/2013"
    ["DESTINATION"]=>
    string(14) "Ridgewood -SEC"
    ["TRACK"]=>
    string(1) "8"
    ["LINE"]=>
    string(4) "BERG"
    ["TRAIN_ID"]=>
    string(4) "1257"
    ["STATUS"]=>
    string(8) "Boarding"
    ["BACKCOLOR"]=>
    string(6) "Silver"
    ["FORECOLOR"]=>
    string(5) "black"
    ["SHADOWCOLOR"]=>
    string(6) "silver"
    ["GPSLATITUDE"]=>
    string(0) ""
    ["GPSLONGITUDE"]=>
    string(0) ""
    ["GPSTIME"]=>
    string(21) "7/18/2013 12:20:34 PM"
    ["TRAIN_LINE"]=>
    string(18) "Bergen County Line"
    ["STATION_POSITION"]=>
    string(1) "0"
    ["LINEABBREVIATION"]=>
    string(4) "BERG"
    ["INLINEMSG"]=>
    string(0) ""
    ["STOPS"]=>
    array(1) {
      ["STOP"]=>
      array(8) {
        [0]=>
        array(2) {
          ["NAME"]=>
          string(18) "Secaucus Lower Lvl"
          ["TIME"]=>
          string(21) "7/18/2013 12:45:30 PM"
        }
        [1]=>
        array(2) {
          ["NAME"]=>
          string(10) "Rutherford"
          ["TIME"]=>
          string(21) "7/18/2013 12:53:15 PM"
        }
        [2]=>
        array(2) {
          ["NAME"]=>
          string(8) "Garfield"
          ["TIME"]=>
          string(21) "7/18/2013 12:58:30 PM"
        }
        [3]=>
        array(2) {
          ["NAME"]=>
          string(12) "Plauderville"
          ["TIME"]=>
          string(20) "7/18/2013 1:01:15 PM"
        }
        [4]=>
        array(2) {
          ["NAME"]=>
          string(18) "Broadway Fair Lawn"
          ["TIME"]=>
          string(20) "7/18/2013 1:06:00 PM"
        }
        [5]=>
        array(2) {
          ["NAME"]=>
          string(17) "Radburn Fair Lawn"
          ["TIME"]=>
          string(20) "7/18/2013 1:09:15 PM"
        }
        [6]=>
        array(2) {
          ["NAME"]=>
          string(19) "Glen Rock Boro Hall"
          ["TIME"]=>
          string(20) "7/18/2013 1:12:30 PM"
        }
        [7]=>
        array(2) {
          ["NAME"]=>
          string(9) "Ridgewood"
          ["TIME"]=>
          string(20) "7/18/2013 1:16:00 PM"
        }
      }
    }
  }
share|improve this question
 
Are STOPS and the like actually a constant defined in your code? Or are you using the auto-constant-to-string "feature" of PHP? If so, you probably shouldn't; instead use "STOPS", etc. –  Waleed Khan Jul 18 '13 at 16:29
1  
Please, use var_export instead of var_dump. –  akond Jul 18 '13 at 16:30
 
@WaleedKhan - I think this was a big issue with the code too, changed this as well. –  mattdonders Jul 18 '13 at 16:36
 
@akond why use var_export instead of var_dump? –  mattdonders Jul 18 '13 at 16:37
1  
@mattdonders So we can copy and paste your data into the code. –  akond Jul 18 '13 at 16:53
add comment

2 Answers

up vote 2 down vote accepted

Your code is broken. You continually reset $stationExists to FALSE every time the loop runs, EXCEPT when you actualy find the station. The code should be

$stationExists = false;
foreach(...) {
   if ($stationName == 'Ridgewood') {
      $stationExists = true;
   }
}

Consider this sequence:

Seacaucus    - no match, $stationExists set to false
Ridgewood    - matched, $stationExists set to TRUE
Plauderville - no match, $stationExists reset to false - oops, now you're stuck

So you set stationExists to false outside the loop, and then run the loop.That way it'll only get changed to true if you really find a match, and never reset to false again.

share|improve this answer
1  
You might as well break or return at that point, too. –  Waleed Khan Jul 18 '13 at 16:31
add comment

Use this function:-

function my_search($haystack) {
    $needle = 'value to search for';
    return(strpos($haystack, $needle)); // or stripos() if you want case-insensitive searching.
}

$matches = array_filter($your_array, 'my_search');

Btw, there is some flaw in your code. There is no need of this else condition else $stationExists = FALSE; inside the loop. Set it as false before the loop runs and if the string matches set it to true.

share|improve this answer
 
I've tried in_array and it still returns FALSE for all entries. –  mattdonders Jul 18 '13 at 16:28
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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