Welcome to WebmasterWorld Guest from 35.173.57.202

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

in_array not working with dynamically populated array

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

Full Member

10+ Year Member

joined:Oct 6, 2004
posts:281
votes: 0


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]

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

Preferred Member

10+ Year Member

joined:July 31, 2006
posts:629
votes: 0


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
)

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

Full Member

10+ Year Member

joined:Oct 6, 2004
posts:281
votes: 0


Thanks JoelGreen.

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

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

Senior Member from US 

WebmasterWorld Senior Member whoisgregg is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Dec 9, 2003
posts:3416
votes: 0


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.
2:12 pm on Apr 4, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 26, 2003
posts:1133
votes: 0


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

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

Full Member

10+ Year Member

joined:Oct 6, 2004
posts:281
votes: 0


@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.

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 16, 2007
posts:914
votes: 0


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.

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members