[英]PHP strcmp & in_array Not Working
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. 我试图看一个数组是否包含一个字符串,但是我遇到了很多问题 - 无论我做什么,它总是返回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.
我想知道在任何时候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. 我已经尝试过
===
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);
}
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"
}
}
}
}
Your code is broken. 你的代码坏了。 You continually reset $stationExists to FALSE every time the loop runs, EXCEPT when you actualy find the station.
每次循环运行时,你都会不断地将$ stationExists重置为FALSE,除非你确实找到了这个站。 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. 所以你在循环之外将stationExists设置为false,然后运行循环。这样,如果你真的找到匹配,它只会变为true,并且永远不会再次重置为false。
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;
没有其他条件,
else $stationExists = FALSE;
inside the loop. 在循环内。 Set it as
false
before the loop runs and if the string matches set it to true
. 在循环运行之前将其设置为
false
,如果字符串匹配,则将其设置为true
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.