Forum Moderators: coopster
Does anyone know why this works:
$list=array('1829','1400','2000');
print_r($list);
// Array ( [0] => 1829 => 1400 [2] => 2000 ) if(in_array('1829',$list)){echo 'true';}else{echo 'false';} // true And this doesn't: while($matches[0][$c]!=''){ $list[$c]=$matches[0][$c]; print_r($list); // Array ( [0] => 1829 [1] => 1400 [2] => 2000 ) if(in_array('1829',$list)){echo 'true';}else{echo 'false';} //false Thanks! [1][edited by: JoaoJose at 11:10 am (utc) on April 4, 2007]
$c++;
}
Turn off strict mode by adding a third parameter:
if( in_array( '1829', $list, FALSE ) ){ echo 'true'; } else { echo 'false'; }
Btw, you don't need to explicitly set the array keys. Empty brackets will cause PHP to automagically set incrementing keys (0,1,2,...)
while($matches[0][$c]!=''){
$list[]=$matches[0][$c];
$c++;
}
Also, how is $c initialized? Typically I would set
$c = 0;before the while loop for the first iteration.
$list = array_filter [de2.php.net]($matches[0]);
and yes, check without types, or convert to integer
Regards
Michal
Turning strict mode to false isn't working.
$c is set to 0;
Thanks for array keys tip.
@mcibor
That's not what I'm trying to do. I just didn't post all the code.
The problem is only what I described. If I manually build an array, the in_array function works. If on the other hand I dinamically populate de array the in_array does not work.
if(in_array(4,$a2))
echo '4 is in $a2';
else
echo '4 is not in $a2';
echo "<br />\n";
I get the true case both times.