From: "Gary ." on
If I have an array that looks like
array(1) {
["mac_address"]=>
string(2) "td"
}

and I call
if (in_array($name, self::$aboveArray))
with $name as
string(11) "mac_address"

what should be the result?

Because it is *false* and it is driving me nuts! This despite the fact
that if I do
$foo = self::$aboveArray[$name];
$foo then has the value
string(2) "td"

Am I not understanding what in_array does? Is there some bug in php that
has been present since 5.2.12 and still is? *bangs head against desk*
From: "Ford, Mike" on
> -----Original Message-----
> From: Gary . [mailto:php-general(a)garydjones.name]
> Sent: 25 June 2010 08:18
> To: PHP
> Subject: [PHP] in_array - what the...
>
> If I have an array that looks like
> array(1) {
> ["mac_address"]=>
> string(2) "td"
> }
>
> and I call
> if (in_array($name, self::$aboveArray))
> with $name as
> string(11) "mac_address"
>
> what should be the result?

FALSE -- in_array checks the *values*, not the keys, so would be looking at the "td" for this element.

To do what you want to do, simply do an isset():

if (isset($array['mac_address'])):
// do stuff with $array['mac_address']
else:
// it doesn't exist
endif;


Cheers!

Mike
--
Mike Ford,
Electronic Information Developer, Libraries and Learning Innovation,
Leeds Metropolitan University, C507, Civic Quarter Campus,
Woodhouse Lane, LEEDS,  LS1 3HE,  United Kingdom
Email: m.ford(a)leedsmet.ac.uk
Tel: +44 113 812 4730





To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm
From: "Gary ." on
"Ford, Mike" writes:
>> -----Original Message-----
>> If I have an array that looks like
>> array(1) {
>> ["mac_address"]=>
>> string(2) "td"
>> }
>>
>> and I call
>> if (in_array($name, self::$aboveArray))
>> with $name as
>> string(11) "mac_address"
>>
>> what should be the result?
>
> FALSE -- in_array checks the *values*, not the keys, so would be
> looking at the "td" for this element.

Agh! So it does.

You know what's worse? I even looked at the documentation of the
function this morning wondering if that's what the problem was and
*still* didn't see it!

*slinks away in embarrassment*
From: "Ford, Mike" on
> -----Original Message-----
> From: Gary . [mailto:php-general(a)garydjones.name]
> Sent: 25 June 2010 09:14
> To: PHP
> Subject: Re: [PHP] in_array - what the...
>
> "Ford, Mike" writes:
> >> -----Original Message-----
> >> If I have an array that looks like
> >> array(1) {
> >> ["mac_address"]=>
> >> string(2) "td"
> >> }
> >>
> >> and I call
> >> if (in_array($name, self::$aboveArray))
> >> with $name as
> >> string(11) "mac_address"
> >>
> >> what should be the result?
> >
> > FALSE -- in_array checks the *values*, not the keys, so would be
> > looking at the "td" for this element.
>
> Agh! So it does.
>
> You know what's worse? I even looked at the documentation of the
> function this morning wondering if that's what the problem was and
> *still* didn't see it!
>
> *slinks away in embarrassment*

Not to worry -- happens to the best of us. (Been there, done that, got a wardrobe full of T-shirts!)

Cheers!

Mike
--
Mike Ford,
Electronic Information Developer, Libraries and Learning Innovation,
Leeds Metropolitan University, C507, Civic Quarter Campus,
Woodhouse Lane, LEEDS,  LS1 3HE,  United Kingdom
Email: m.ford(a)leedsmet.ac.uk
Tel: +44 113 812 4730





To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm
From: "Daevid Vincent" on

> -----Original Message-----
> From: Gary . [mailto:php-general(a)garydjones.name]
> Sent: Friday, June 25, 2010 1:14 AM
> To: PHP
> Subject: Re: [PHP] in_array - what the...
>
> "Ford, Mike" writes:
> >> -----Original Message-----
> >> If I have an array that looks like
> >> array(1) {
> >> ["mac_address"]=>
> >> string(2) "td"
> >> }
> >>
> >> and I call
> >> if (in_array($name, self::$aboveArray))
> >> with $name as
> >> string(11) "mac_address"
> >>
> >> what should be the result?
> >
> > FALSE -- in_array checks the *values*, not the keys, so would be
> > looking at the "td" for this element.
>
> Agh! So it does.
>
> You know what's worse? I even looked at the documentation of the
> function this morning wondering if that's what the problem was and
> *still* didn't see it!
>
> *slinks away in embarrassment*

Why do this "in_array()" business??

Just do this...

if (self::$aboveArray[$name])
{
//something interesting here
}