FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » why does in_array return true
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
why does in_array return true [message #169869] Wed, 29 September 2010 01:24 Go to next message
heartmeat is currently offline  heartmeat
Messages: 1
Registered: September 2010
Karma: 0
Junior Member
hello all

I must be missing something obvious, but, given:

var $haystack = array(0);
var $needle = '0_50';

why does:

in_array($needle,$haystack) return true?

(PHP Version 5.2.4-2ubuntu5.10)

thanks.
Re: why does in_array return true [message #169870 is a reply to message #169869] Wed, 29 September 2010 01:47 Go to previous messageGo to next message
rf is currently offline  rf
Messages: 19
Registered: September 2010
Karma: 0
Junior Member
"heartmeat" <brendon(at)actrix(dot)co(dot)nz> wrote in message
news:d1fd2435-5e14-4d9f-93db-096bea63fbd7(at)p22g2000pre(dot)googlegroups(dot)com...
> hello all
>
> I must be missing something obvious, but, given:
>
> var $haystack = array(0);
> var $needle = '0_50';
>
> why does:
>
> in_array($needle,$haystack) return true?

This does not even parse. Unexpected 'var'.

Assuming what you meant is

$haystack = array(0);
$needle = '0_50';

then think about what in_array is doing internally. It's comparing the value
of $needle with each element of the array $haystack looking for a match.
It's a bit like

for ($i = 0; $i < count($haystack); $i++)
{
if ($needle == $haystack[$i])
return true;
}
return false;

Now, what is the result of ($needle == $haystack[0]) ?. True of course.

$haystack[0] is a number, so the comparison will cause $needle to be
converted to a number, along the lines of intval($needle). Now, $needle is
"0_50" and intval($needle) is, of course, 0. If $needle was "1_50" then
intval($needle) would be 1.

Did you perchance forget to include the third parameter of in_array, bool
$strict ? If you had then the above comparison would have been
if ($needle === $haystack[$i])
which of course false whenr $i is 0 as $needle and $haystack[0] are of
different type.
Re: why does in_array return true [message #169871 is a reply to message #169869] Wed, 29 September 2010 01:56 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On 29/09/10 02:24, heartmeat wrote:
> hello all
>
> I must be missing something obvious, but, given:
>
> var $haystack = array(0);
> var $needle = '0_50';
>
> why does:
>
> in_array($needle,$haystack) return true?

You didn't specify strict checking, so the string equates to 0.

Rgds

Denis McMahon
Re: why does in_array return true [message #169872 is a reply to message #169869] Wed, 29 September 2010 01:46 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 9/28/2010 9:24 PM, heartmeat wrote:
> hello all
>
> I must be missing something obvious, but, given:
>
> var $haystack = array(0);
> var $needle = '0_50';
>
> why does:
>
> in_array($needle,$haystack) return true?
>
> (PHP Version 5.2.4-2ubuntu5.10)
>
> thanks.

Because 0 is an integer and 0_50 is a string. See

http://www.php.net/manual/en/language.types.type-juggling.php

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: why does in_array return true [message #169882 is a reply to message #169869] Wed, 29 September 2010 10:23 Go to previous message
Geoff Berrow is currently offline  Geoff Berrow
Messages: 16
Registered: September 2010
Karma: 0
Junior Member
On Tue, 28 Sep 2010 18:24:58 -0700 (PDT), heartmeat
<brendon(at)actrix(dot)co(dot)nz> wrote:

> I must be missing something obvious, but, given:
>
> var $haystack = array(0);
> var $needle = '0_50';

var is not used in PHP. Some confusion with JavaScript?

Are you perhaps expecting array(0) to give you and empty array? It
doesn't, it gives you an array with one element.

An empty array would be:
$haystack = array();

>
> why does:
>
> in_array($needle,$haystack) return true?

See other answers.
--
Geoff Berrow (Put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs www.4theweb.co.uk/rfdmaker
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: PHP manual note
Next Topic: Out Of Memory with Memory_limit à -1
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Sep 20 20:31:18 GMT 2024

Total time taken to generate the page: 0.02876 seconds