473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

in_array() wierdness with arrays inside of $_POST

I am using in_array() to search for a value ("other"), in order to validate
a form.
If I pass $_POST["interests"] as the array to search PHP says that it is an
invalid datatype.
It is an array and if I copy it to another variable (i.e. $list =
$_POST["interests"];) it works fine.
The same is also true of implode().

Do you believe this is intended or is it a bug?
--
AJ Zmudosky
There are no stupid questions, but there are a LOT of inquisitive idiots.
Jul 17 '05 #1
12 5977
AJ Z wrote:
I am using in_array() to search for a value ("other"), in order to
validate a form.
If I pass $_POST["interests"] as the array to search PHP says that it is
an invalid datatype.
It is an array and if I copy it to another variable (i.e. $list =
$_POST["interests"];) it works fine.
The same is also true of implode().

Do you believe this is intended or is it a bug?
--
AJ Zmudosky
There are no stupid questions, but there are a LOT of inquisitive idiots.


$_POST['whatever'] isn't an array $_POST is an array, but yes you will
have to make it into an array first using something like

$data = explode(",", $_POST['interests']);

then it's an array.

~Cameron
Jul 17 '05 #2
On Sun, 01 Feb 2004 17:50:37 +0000, Cameron <fo*@bar.invalid> wrote:
AJ Z wrote:
I am using in_array() to search for a value ("other"), in order to
validate a form.
If I pass $_POST["interests"] as the array to search PHP says that it is
an invalid datatype.
It is an array and if I copy it to another variable (i.e. $list =
$_POST["interests"];) it works fine.
The same is also true of implode().

Do you believe this is intended or is it a bug?


$_POST['whatever'] isn't an array $_POST is an array, but yes you will
have to make it into an array first using something like

$data = explode(",", $_POST['interests']);

then it's an array.


$_POST['whatever'] _can_ be an array. The OP said it was.

Consider:

<input type="checkbox" name="whatever[]" value="one">
<input type="checkbox" name="whatever[]" value="two">
<input type="checkbox" name="whatever[]" value="three">

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
<http://www.andyh.co.uk> / <http://www.andyhsoftware.co.uk/space>
Jul 17 '05 #3
On Sun, 1 Feb 2004 09:19:32 -0800, AJ Z <us****@gwws.net> wrote:
I am using in_array() to search for a value ("other"), in order to validate
a form.
If I pass $_POST["interests"] as the array to search PHP says that it is an
invalid datatype.
It is an array and if I copy it to another variable (i.e. $list =
$_POST["interests"];) it works fine.
The same is also true of implode().

Do you believe this is intended or is it a bug?


Are you sure it's an array? Use var_dump, or is_array to check.

Is it produced by a series of input elements named "whatever[]" (note the use
of [] - required for PHP to produce an array)?

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
<http://www.andyh.co.uk> / <http://www.andyhsoftware.co.uk/space>
Jul 17 '05 #4
On 2004-02-01 10:18:17 -0800, Andy Hassall <an**@andyh.co.uk> said:
On Sun, 1 Feb 2004 09:19:32 -0800, AJ Z <us****@gwws.net> wrote:
I am using in_array() to search for a value ("other"), in order to validate >a form.If I pass $_POST["interests"] as the array to search PHP says that it is an >invalid datatype.It is an array and if I copy it to another variable (i.e. $list =$_POST["interests"];) it works fine.
The same is also true of implode().

Do you believe this is intended or is it a bug?


Are you sure it's an array? Use var_dump, or is_array to check.

Is it produced by a series of input elements named "whatever[]" (note

the use of [] - required for PHP to produce an array)?

-- Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
<http://www.andyh.co.uk> / <http://www.andyhsoftware.co.uk/space>


Yes, it is an array, I used is_array and var_dump at the suggestion of
others, but I am wondering why it doesn't work as $_POST["interests"], but
it does as $list (if $list = $_POST["interests"] -- it should be exactly
the same). It is an array due to what you suggested to Cameron (checkboxes
with a name of "something[]")
--
AJ Zmudosky
There are no stupid questions, but there are a LOT of inquisitive idiots.
Jul 17 '05 #5
On Sun, 1 Feb 2004 13:39:48 -0800, AJ Z <us****@gwws.net> wrote:
On 2004-02-01 10:18:17 -0800, Andy Hassall <an**@andyh.co.uk> said:
On Sun, 1 Feb 2004 09:19:32 -0800, AJ Z <us****@gwws.net> wrote:
I am using in_array() to search for a value ("other"), in order to
validate a form.
If I pass $_POST["interests"] as the array to search PHP says that it is
an invalid datatype.
It is an array and if I copy it to another variable (i.e. $list =
$_POST["interests"];) it works fine.
The same is also true of implode().

Do you believe this is intended or is it a bug?


Are you sure it's an array? Use var_dump, or is_array to check.

Is it produced by a series of input elements named "whatever[]" (note
the use of [] - required for PHP to produce an array)?


Yes, it is an array, I used is_array and var_dump at the suggestion of
others, but I am wondering why it doesn't work as $_POST["interests"], but
it does as $list (if $list = $_POST["interests"] -- it should be exactly
the same). It is an array due to what you suggested to Cameron (checkboxes
with a name of "something[]")


Please post a small runnable example that demonstrates your problem.

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
<http://www.andyh.co.uk> / <http://www.andyhsoftware.co.uk/space>
Jul 17 '05 #6
Andy Hassall <an**@andyh.co.uk> wrote in message news:<67********************************@4ax.com>. ..
On Sun, 1 Feb 2004 13:39:48 -0800, AJ Z <us****@gwws.net> wrote:
On 2004-02-01 10:18:17 -0800, Andy Hassall <an**@andyh.co.uk> said:
On Sun, 1 Feb 2004 09:19:32 -0800, AJ Z <us****@gwws.net> wrote:

I am using in_array() to search for a value ("other"), in order to
validate a form.
If I pass $_POST["interests"] as the array to search PHP says that it is
an invalid datatype.
It is an array and if I copy it to another variable (i.e. $list =
$_POST["interests"];) it works fine.
The same is also true of implode().

Do you believe this is intended or is it a bug?

Are you sure it's an array? Use var_dump, or is_array to check.

Is it produced by a series of input elements named "whatever[]" (note
the use of [] - required for PHP to produce an array)?


Yes, it is an array, I used is_array and var_dump at the suggestion of
others, but I am wondering why it doesn't work as $_POST["interests"], but
it does as $list (if $list = $_POST["interests"] -- it should be exactly
the same). It is an array due to what you suggested to Cameron (checkboxes
with a name of "something[]")


Please post a small runnable example that demonstrates your problem.


If it is verified by *is_array* that $_POST["interests"] is an array,
then i guess you must be sending arguments in wrong order.
Please verify the syntax of *in_array*:

bool in_array ( mixed needle, array haystack [, bool strict] )

post a sample code if it still not solved.

--
Cheers,
Rahul Anand
Jul 17 '05 #7
On 2004-02-01 13:52:47 -0800, Andy Hassall <an**@andyh.co.uk> said:
...
Please post a small runnable example that demonstrates your problem.
--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
<http://www.andyh.co.uk> / <http://www.andyhsoftware.co.uk/space>

<?php
if ($_POST["action"] == "doprocform") {
// Remove excess spaces
foreach ($_POST as $key => $val) { $_POST[$key] = trim($val); }
// Validate form
// Do other checks here
if (in_array("other", $_POST["interests"]) && $_POST["other_explanation"]
== "") { $err = true; }
// Compress listing arrays
$interests_list = implode(", ", $_POST["interests"]);
if ($err) {
// Display error message(s)
} else {
// Transmit form (via e-mail or to DB, etc.)
}
} else {
// Display form (STND. HTML)
}
?>
The above use of in_array() and implode() generates warnings, under both
PHP 4.3.3 and PHP 5.0.0b3.
--
AJ Zmudosky
There are no stupid questions, but there are a LOT of inquisitive idiots.
Jul 17 '05 #8
AJ Z <us****@gwws.net> wrote in message news:<2004020122060316807%usenet@gwwsnet>...
On 2004-02-01 13:52:47 -0800, Andy Hassall <an**@andyh.co.uk> said:
..
Please post a small runnable example that demonstrates your problem.
--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
<http://www.andyh.co.uk> / <http://www.andyhsoftware.co.uk/space> <?php
if ($_POST["action"] == "doprocform") {
// Remove excess spaces
foreach ($_POST as $key => $val) { $_POST[$key] = trim($val); }


The problem is here
*****************************
$_POST[$key] = trim($val);
*****************************

You can not trim an array. If you enable the notice errors, (a good
practice to catch errors during development) you will get an error
from PHP Parser.

In your case the $val will be converted to string and trim function
will return a string value "Array".

Modify the lines with:

foreach ($_POST as $key => $val)
{
if(!is_array($val))
$_POST[$key] = trim($val);
}

// Validate form
// Do other checks here
if (in_array("other", $_POST["interests"]) && $_POST["other_explanation"]
== "") { $err = true; }
// Compress listing arrays
$interests_list = implode(", ", $_POST["interests"]);
if ($err) {
// Display error message(s)
} else {
// Transmit form (via e-mail or to DB, etc.)
}
} else {
// Display form (STND. HTML)
}
?>
The above use of in_array() and implode() generates warnings, under both
PHP 4.3.3 and PHP 5.0.0b3.

Jul 17 '05 #9
On 2004-02-01 22:25:57 -0800, ra************@rediffmail.com (Rahul Anand)
said:
...
post a sample code if it still not solved.

--
Cheers,
Rahul Anand


Even with your suggestion on trim() I get the warning. Confirmed in_array()
args are in the correct order.
--
AJ Zmudosky
There are no stupid questions, but there are a LOT of inquisitive idiots.
Jul 17 '05 #10
AJ Z <us****@gwws.net> wrote in message news:<2004020219572116807%usenet@gwwsnet>...
On 2004-02-01 22:25:57 -0800, ra************@rediffmail.com (Rahul Anand)
said:
..
post a sample code if it still not solved.

--
Cheers,
Rahul Anand


Even with your suggestion on trim() I get the warning. Confirmed in_array()
args are in the correct order.


It works at my system [PHP 5]

[SNIP]

$_POST['key'] = array('aKey'=>'aValue','bKey'=>'bValue');

// Remove excess spaces
foreach ($_POST as $key => $val)
if(!is_array($val))
$_POST[$key] = trim($val);

print_r($_POST);
exit;

[/SNIP]

Run this code to check it working. If it works, then receive the array
from a form post. I assure you that, it will execute smoothly.

You might have coded wrong, so please post the code you are trying to
execute.

--
Cheers,
Rahul Anand
Jul 17 '05 #11
On 2004-02-03 00:59:23 -0800, ra************@rediffmail.com (Rahul Anand)
said:
...

Your code does execute without errors, and shows up.
I also ran it through in_array (searching for 'aValue') and it generated no
warnings.
However, my array also returns true for is_array(), but gives the warning.
The entire source code for the form can be seen at:
http://www.gwws.net/form.phps
If you would like it posted here I can trim it down and post it.
--
AJ Zmudosky
There are no stupid questions, but there are a LOT of inquisitive idiots.
Jul 17 '05 #12
AJ Z <us****@gwws.net> wrote in message news:<2004020307093975249%usenet@gwwsnet>...
On 2004-02-03 00:59:23 -0800, ra************@rediffmail.com (Rahul Anand)
said:
..

Your code does execute without errors, and shows up.
I also ran it through in_array (searching for 'aValue') and it generated no
warnings.
However, my array also returns true for is_array(), but gives the warning.
The entire source code for the form can be seen at:
http://www.gwws.net/form.phps
If you would like it posted here I can trim it down and post it.


I checked the code and found the reason for the error you are getting.
Here goes the explanation:

Line 10:
[SNIP]

if (is_array($_POST["interests"])) { echo "'interests' IS AN ARRAY<br
/>\n"; } else { echo "'interests' IS AN ARRAY<br />\n"; }

[/SNIP]

In both case you are printing "'interests' IS AN ARRAY".
Change the else part, and you will notice *interests* is not array in
all cases.

Reason:
PHP forms an array only when there is atleast one checkbox checked.
Because a form only sends the checked checkbox values.

Solution:
Before trying to operate on intersts array you need to test the array
with *isempty* or *isset* or *isarray*

Hope it will help.

--
Rahul Anand
Jul 17 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Bart Nessux | last post by:
How is this done in php? I've tried several variations of the following: contact_array = ($_POST, $_POST, $_POST, $_POST, $_POST); The ultimate goal is to insert the array into a table in...
3
by: Phil Powell | last post by:
if (is_array($_POST)) { foreach ($this->getAssocSectionsObjArray($key, $dbAP) as $obj) { print_r($obj); print_r(" in array? "); print_r(in_array($obj, $result)); print_r("<P>"); if...
8
by: Oeln | last post by:
If I want to check for input of an integer I've got the following (I get the form input with $input = "$_POST"): if(!ereg("^+$",$_POST)) { echo "Input is incomplete or incorrect."; } If,...
5
by: Phil Powell | last post by:
Here is my array. Plain and simple enumerative array with values being strings. So why does this fail??? print_r(in_array('album', array_keys($boolword))); This produces FALSE or NULL....
6
by: Fnark! | last post by:
I am creating a shopping cart using PHP Version 4.1.2. I am creating and registering a cart object in a session. The cart object contains an array of arrays called $order whose elements are a...
8
by: mantrid | last post by:
Hello Im having problems working out why the following code does not work. I dont think its the sql as the error occurs on the first update which ever one is put there ($q1 or $q2). Ive swapped...
0
by: rich | last post by:
I have 2 arrays. I am trying to use in_array with and it doesn't seem to be working. I have done a print_r of each one for my test data. Array $primsecA is below: Array ( =Array ( =1 ...
3
by: Sonnich | last post by:
The following code is an exact copy of my current code. The idea is to check 2 parts (of string arrays), and add those only once to a common array. I check for existance of an array in a string,...
0
by: Jim Carlock | last post by:
$aThePosts = array_change_key_case($_POST, CASE_LOWER); define("CONTACT_IS_LOCAL", 0); define("CONTACT_IS_REMOTE", 1); /* $aWho contains an array of arrays (contact details) * array( *...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.