This is killing me. It seems so simple, but for some reason I cannot get it to work.
Basically what I have is a function that accepts a single string $name
as a variable, then it goes through and checks that variable against values in an array. If the variable is found in the array is returns TRUE
. This function is called from within a foreach loop in another function (which submits a new $name
variable each time.)
I have tried 3 different ways to check the variable against the array but it never works properly.
1.) using another foreach()
loop
function check($name) {
$commaSeparatedString = 'First Name, Second Name, Third Name';
$array = explode(",", $commaSeparatedString);
foreach($array as $arrayValue) {
if($arrayValue == $name) {
return TRUE;
}
else {
return FALSE;
}
}
}
2.) using in_array()
function check($name) {
$commaSeparatedString = 'First Name, Second Name, Third Name';
$array = explode(",", $commaSeparatedString);
if(in_array($name, $array, TRUE)) {
return TRUE;
}
else {
return FALSE;
}
}
3.) looping through each value in the array manually and checking for a match
function check($name) {
$commaSeparatedString = 'First Name, Second Name, Third Name';
$array = explode(",", $commaSeparatedString);
$count = count($array);
$i = 0;
while($i<=$count) {
if(isset($array[$i]) &a开发者_如何学编程mp;& $array[$i] == $name) {
$i++;
echo $i;
return TRUE;
break;
}
else {
$i++;
echo $i;
return FALSE; }
}
}
Here a simplified part of the function where check()
is run:
function loopCheck() {
$group = array($obj1, $obj2, $obj3, $obj4, $obj5);
foreach($group as $groupValue) {
$name = $groupValue->name;
$ignore = $this->check($name);
if($ignore == TRUE) { }
else { echo $name; }
}
}
The result for all three variations of check()
is the same. It returns TRUE
for every $groupValue
except the first value of $array
. So if the $name
of $obj2
was 'First Name' it would return TRUE
, but if the $name
of $obj3
was 'Second Name' is still returns FALSE
. I echoed $i
for the third variation at one point and the value consistently stayed at 1, so I know there is some error with that method, but I don't know why the result would be the same using the other 2 methods.
There are several issues with your code. The one that causes the failure of all code, is that you explode
by ','
, thus leaving a whitespace in your strings. You should explode
on ', '
.
But the first code is erroneous still: The foreach
will return on the first iteration, always, thus checking only the first element. The loop should be:
foreach ($array as $arrayValue) {
if ($arrayValue == $name) {
return true;
}
}
return false;
Same applies to your last code.
The best variant is probably the second, here is a slightly shorter, adjusted variant:
function check($name) {
$commaSeparatedString = 'First Name, Second Name, Third Name';
return in_array($name, explode(', ', $commaSeparatedString), true));
}
You should not put
Return false;
On your else, you should put it after the whole loop. Also keep in mind that if you wanna explode a, b, c to be [a,b,c] you have to use
explode(", ",$str); //note the space
The problem is that when you make the explode in the check function, some pieces have spaces because there is a space after the comma.
Did you trim() your strings?
It would work on the first string as it starts with "First String" but the second explode()ed string would be "spaceSecond String".
I think any of those techniques will work, but you just have small errors.
I've fixed the first one below. You don't want to return false
in an else clause. you want it AFTER the foreach loop, only if it fails to match EVERY time.
function check($name) {
$commaSeparatedString = 'First Name, Second Name, Third Name';
$array = explode(",", $commaSeparatedString);
foreach($array as $arrayValue) {
if($arrayValue == $name) {
return TRUE;
}
}
return false;
}
The in_array version is potentially simpler. Notice that there is no reason for if ($val) {return true;} else {return false;}
, just do return $val
function check($name)
{
$csvs = 'First Name, Second Name, Third Name';
return in_array($name, explode(', ', $csv));
//return in_array(trim(strtoupper($name)), array_map('trim', explode(', ', strtoupper($csv))));
//try this second way is kind of over-kill thorough, but try it if you still get false where true is expected
}
You also have to either trim
the strings of the array you make or explode(', ' $csv)
with a space. Otherwise the elements of array
will have a leading space.
精彩评论