Forum Moderators: coopster

Message Too Old, No Replies

in array problem

checkboxes

         

rokec

8:05 pm on Feb 27, 2007 (gmt 0)

10+ Year Member



why

in_array("new", $_POST['checkboxes']);

doesn't work?

eelixduppy

8:08 pm on Feb 27, 2007 (gmt 0)



Hello,

Your syntax is correct and should work as written. Can you please be a little more specific in terms of what your problem is?

Thanks

winglian

8:24 pm on Feb 27, 2007 (gmt 0)

10+ Year Member



Do you have the following as the code for your checkbox?

<input type="checkbox" name="checkboxes[]" value="new" />New
<input type="checkbox" name="checkboxes[]" value="etc" />Etc

dreamcatcher

9:22 pm on Feb 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



rokec,

Firstly, try setting your error reporting level to E_ALL to help out with debugging. Its probably a case of you missing the [] operators as mentioned by winglian.

You can test if your array is an array by using is_array() [uk2.php.net]

if (is_array($_POST['checkboxes']))
{
// proceed
}
else
{
//uh oh, somethings wrong
}

dc

rokec

9:24 pm on Feb 27, 2007 (gmt 0)

10+ Year Member



oh, i missed something:
in_array("new", sprintf("%02d", $_POST['delete'])

should this work? If not, how to do that it will work?

eelixduppy

9:28 pm on Feb 27, 2007 (gmt 0)



sprintf [us2.php.net] returns a string, and in_array requires an array, so no, this will not work. Do you instead want to use strpos [us2.php.net] to determine if "new" is contained within the string?

rokec

9:31 pm on Feb 27, 2007 (gmt 0)

10+ Year Member



$_POST[delete] is an array, got by checkboxes.

scriptmasterdel

9:57 pm on Feb 27, 2007 (gmt 0)

10+ Year Member



As eelixduppy mentioned, the function "sprintf" returns a string so when you try and find the value "new" in the array the function will fail because the second argument isn't an array.

You could convert the format of each value like this before comparing the strings.

foreach ($_POST[delete] as $key => $value)
$_POST[delete][$key] = sprintf("%02d", $value);

Then for the comparison.

in_array("new", $_POST['delete']);

Please note: Untested code but i hope this helps.

Del