Forum Moderators: coopster

Message Too Old, No Replies

in_array not working with dynamically populated array

         

JoaoJose

11:10 am on Apr 4, 2007 (gmt 0)

10+ Year Member



Hi.

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];
$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]

joelgreen

12:07 pm on Apr 4, 2007 (gmt 0)

10+ Year Member



Maybe there is some \n character?
Try replacing this
$list[$c]=$matches[0][$c];
with this
$list[$c]=trim($matches[0][$c]);

Not very related, but maybe you'll find this interesting:

$list=array('1829','1400','2000',1829);

print_r($list);

Array
(
[0] => 1829
[1] => 1400
[2] => 2000
[3] => 1829
)

JoaoJose

1:20 pm on Apr 4, 2007 (gmt 0)

10+ Year Member



Thanks JoelGreen.

I had already thought of that...doesn't work.

whoisgregg

1:54 pm on Apr 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Perhaps in_array is in strict mode and is returning false because the second array contains integers instead of strings?

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.

mcibor

2:12 pm on Apr 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also I see, that you just want to remove empty values from an array. it's done easily with:

$list = array_filter [de2.php.net]($matches[0]);

and yes, check without types, or convert to integer

Regards
Michal

JoaoJose

2:33 pm on Apr 4, 2007 (gmt 0)

10+ Year Member



@whoisgregg

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.

cameraman

5:59 pm on Apr 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know why your example isn't working, but I don't think the problem is in in_array - this works:
$ary = array(2,4,6,8,10);
$a2 = array();
foreach($ary as $val)
$a2[] = $val;
if(in_array(4,$ary))
echo '4 is in $ary';
else
echo '4 is not in $ary';
echo "<br />\n";

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.