By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,748 Members | 1,451 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,748 IT Pros & Developers. It's quick & easy.

in_array() fails but it's in the array

P: n/a
array_keys($boolword) = Array ( [0] => 'album' [1] => 'keywords' [2]
=> 'persons' [3] => 'events' [4] => 'image_alt' [5] =>
'image_creation_start_date' [6] => 'image_creation_end_date' [7] =>
'image_location_city' [8] => 'image_location_state' [9] =>
'image_location_country' )
Here is my array. Plain and simple enumerative array with values
being strings.

So why does this fail???

[PHP]
print_r(in_array('album', array_keys($boolword)));
[/PHP]

This produces FALSE or NULL. But it's right there IN the array!! What
on earth is going on? Is this a failure in the part of in_array() that
I am unaware of?

This is a major showstopper in my search code PHP script I'm writing
for my application, please help someone!

Thanx
Phil
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
have you tried with the proper syntax?

array_keys($boolword) = Array ( [0] => 'album',
[1] => 'keywords',
[2] => 'persons',
[3] => 'events',
[4] => 'image_alt',
[5] => 'image_creation_start_date',
[6] => 'image_creation_end_date',
[7] => 'image_location_city',
[8] => 'image_location_state',
[9] => 'image_location_country' );

R

--
Due to VERY heavy spam reception (24,000+/week), I use a fake address.
Please write me in this newsgroup if you want to get in contact with me

NEVER support spammers or companies sending out spam
NEVER use links sent to you in a spam mail
NEVER unsubscribe a spam message
NEVER reply to spam

"Phil Powell" <so*****@erols.com> wrote in message
news:1c**************************@posting.google.c om...
> array_keys($boolword) = Array ( [0] => 'album' [1] => 'keywords' [2]
=> 'persons' [3] => 'events' [4] => 'image_alt' [5] =>
'image_creation_start_date' [6] => 'image_creation_end_date' [7] =>
'image_location_city' [8] => 'image_location_state' [9] =>
'image_location_country' )

Here is my array. Plain and simple enumerative array with values
being strings.

So why does this fail???

[PHP]
print_r(in_array('album', array_keys($boolword)));
[/PHP]

This produces FALSE or NULL. But it's right there IN the array!! What
on earth is going on? Is this a failure in the part of in_array() that
I am unaware of?

This is a major showstopper in my search code PHP script I'm writing
for my application, please help someone!

Thanx
Phil

Jul 17 '05 #2

P: n/a
Phil Powell a écrit:
> array_keys($boolword) = Array ( [0] => 'album' [1] => 'keywords' [2]
=> 'persons' [3] => 'events' [4] => 'image_alt' [5] =>
'image_creation_start_date' [6] => 'image_creation_end_date' [7] =>
'image_location_city' [8] => 'image_location_state' [9] =>
'image_location_country' )

Here is my array. Plain and simple enumerative array with values
being strings.

So why does this fail???

[PHP]
print_r(in_array('album', array_keys($boolword)));
[/PHP]


What is $boolword ? it's inconsistent here, just place the name of the
array as second arg : print_r(in_array('album', array_keys));

Jul 17 '05 #3

P: n/a
In article <1c**************************@posting.google.com >,
so*****@erols.com (Phil Powell) wrote:
> array_keys($boolword) = Array ( [0] => 'album' [1] => 'keywords' [2]
=> 'persons' [3] => 'events' [4] => 'image_alt' [5] =>
'image_creation_start_date' [6] => 'image_creation_end_date' [7] =>
'image_location_city' [8] => 'image_location_state' [9] =>
'image_location_country' )

Here is my array. Plain and simple enumerative array with values
being strings.

So why does this fail???

[PHP]
print_r(in_array('album', array_keys($boolword)));
[/PHP]


You probably mean something like this:

$boolword = array('album', 'keywords', ...);

var_dump(in_array('album', $boolword));

JP

--
Sorry, <de*****@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.
Jul 17 '05 #4

P: n/a
No, sorry I meant exactly this:

Expand|Select|Wrap|Line Numbers
  1. $html .= "<select name=\"boolword['album']\">\n";
  2. ....
  3.  
And to handle it I would parse $_POST out to get $boolword and you get
this:

Expand|Select|Wrap|Line Numbers
  1. print_r($boolword);
  2.  
Results:

Array ('album' => 'AND', 'location' => '', 'image_name' => 'OR',
'whatever' => '')
Sorry I wasn't more clear. $boolword will be an associative array
whose keys are string values that are dynamically generated from the
name of the HTML form dropdown element 'boolword[something]'; the
value being a boolean word.

Problem is this: If I try to reference $boolword[$elementName] where
$elementName = 'album', it fails:

Expand|Select|Wrap|Line Numbers
  1. print_r($boolword[$elementName]);
  2.  
Results: NULL

However, if I reference $boolword["'$elementName'"] where $elementName
= 'album', it works perfectly:

Expand|Select|Wrap|Line Numbers
  1. print_r($boolword["'$elementName'"]);
  2.  
Results: AND

This is what I'm trying to convey. The associative array $boolword
produces an array where the keys seem to have embedded single quotes
inside the keys themselves!! Is this a PHP bug?

Phil

Jan Pieter Kunst <de*****@cauce.org> wrote in message news:<de***************************@news1.news.xs4 all.nl>...
In article <1c**************************@posting.google.com >,
so*****@erols.com (Phil Powell) wrote:
> > array_keys($boolword) = Array ( [0] => 'album' [1] => 'keywords' [2]
=> 'persons' [3] => 'events' [4] => 'image_alt' [5] =>
'image_creation_start_date' [6] => 'image_creation_end_date' [7] =>
'image_location_city' [8] => 'image_location_state' [9] =>
'image_location_country' )

Here is my array. Plain and simple enumerative array with values
being strings.

So why does this fail???

[PHP]
print_r(in_array('album', array_keys($boolword)));
[/PHP]


You probably mean something like this:

$boolword = array('album', 'keywords', ...);

var_dump(in_array('album', $boolword));

JP

Jul 17 '05 #5

P: n/a
In article <1c*************************@posting.google.com> ,
so*****@erols.com (Phil Powell) wrote:
Expand|Select|Wrap|Line Numbers
  1.  $html .= "<select name=\"boolword['album']\">\n";
  2.  ...
  3.  

[...]
This is what I'm trying to convey. The associative array $boolword
produces an array where the keys seem to have embedded single quotes
inside the keys themselves!! Is this a PHP bug?

Phil


No, it's because you put the single quotes in the HTML. If you want to
use [] syntax to get an array into $_REQUEST, you can either do this:

<select name="boolword[]">

for a numeric array $_REQUEST['boolword'], or this:

<select name="boolword[album]">

for an associative array $_REQUEST['boolword']. You shouldn't quote the
array key in the HTML. Quoting array keys is only needed in PHP code.

JP

--
Sorry, <de*****@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.