【问题标题】:PHP strcmp & in_array Not WorkingPHP strcmp & in_array 不工作
【发布时间】:2013-07-18 16:25:46
【问题描述】:

我正在尝试查看一个数组是否包含一个字符串,但是遇到了很多问题 - 无论我做什么,它总是返回 false。

基本上我想看看火车上的停靠点是否与特定字符串匹配。我正在尝试查看是否在任何时候 NAME = Ridgewood。

下面是我正在使用的代码,因为它是一个多维数组。数组在代码下方。我试过===in_array,甚至strcmp,但每次都得到FALSE。

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);
}

这是我正在搜索的数组:

[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"
        }
      }
    }
  }

【问题讨论】:

  • STOPS 等实际上是在您的代码中定义的常量吗?或者您是否使用 PHP 的自动常量字符串“功能”?如果是这样,您可能不应该这样做;而是使用"STOPS"等。
  • 请使用 var_export 而不是 var_dump。
  • @WaleedKhan - 我认为这也是代码的一个大问题,也改变了这一点。
  • @akond 为什么使用var_export 而不是var_dump
  • @mattdonders 所以我们可以将您的数据复制并粘贴到代码中。

标签: php arrays string multidimensional-array


【解决方案1】:

您的代码已损坏。每次循环运行时,您都会不断地将 $stationExists 重置为 FALSE,除非您实际找到该站。代码应该是

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

考虑这个顺序:

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

所以你在循环外将 stationExists 设置为 false,然后运行循环。这样只有在你真的找到匹配项时才会将其更改为 true,并且永远不会再次重置为 false。

【讨论】:

    【解决方案2】:

    使用这个功能:-

    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');
    

    顺便说一句,您的代码中存在一些缺陷。循环内不需要这个 else 条件else $stationExists = FALSE;。在循环运行之前将其设置为false,如果字符串匹配,则将其设置为true

    【讨论】:

    • 我已经尝试过 in_array 并且它仍然为所有条目返回 FALSE。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-01
    • 2016-06-23
    • 2019-01-26
    相关资源
    最近更新 更多