why does in_array return true [message #169869] |
Wed, 29 September 2010 01:24 |
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 |
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 |
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 #169882 is a reply to message #169869] |
Wed, 29 September 2010 10:23 |
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
|
|
|