You could do something like the following.
Code: Select all
$Nothing = true;
foreach ($_POST as $Field=>$Value)
$Nothing = $Nothing && strlen($Value) == 0;Moderator: General Moderators
Code: Select all
$Nothing = true;
foreach ($_POST as $Field=>$Value)
$Nothing = $Nothing && strlen($Value) == 0;Code: Select all
<?php
if ((!isset($_POST['var'])) || (empty($_POST['var']))) {
// do something in response to $_POST['var'] not being complete
}
?>Well, the array_filter() idea is interesting, but that requires writing a separate function. So, its complexity is definitely greater than that of the foreach solution (which already seems too complex for such a simple task).jshpro2 wrote:You could work something out with http://us2.php.net/manual/en/function.array-filter.php
Or just put your code into a function..
You could also call array_unique which will remove all the duplicates (leaving you with an array of one empty element)
Yeah, but the array is not actually empty. Instead, the string in the array is empty (and we don't now the name of the array element that holds the string). I'm sure the code is very simple, I'm just not sure exactly what it looks like (and I confess I'm really a Java developer not a PHP developer).jshpro2 wrote:empty()pilaftank wrote:But how would I actually test that in a boolean statement?
Code: Select all
$filtered = array_filter($array, 'strlen');Code: Select all
reset($array);
var_dump(empty(current($array)));pilaftank wrote:Yeah, but the array is not actually empty. Instead, the string in the array is empty (and we don't now the name of the array element that holds the string). I'm sure the code is very simple, I'm just not sure exactly what it looks like (and I confess I'm really a Java developer not a PHP developer).jshpro2 wrote:empty()pilaftank wrote:But how would I actually test that in a boolean statement?
Code: Select all
<?php
foreach($_POST as $post_key => $post_val) {
if ( empty($post_val) ) {
echo "POST VAR key $post_key is empty!<br />";
}
}
?>Code: Select all
<?php
$test_array = array(
"key1" => '',
"key2" => '',
"key3" => '',
"key4" => '',
"key5" => '');
?>If the form was submitted by way of a button and the button had any text on it, that button will be sent as a POST var and it will set its value in the POST array. If there were any other fields in the form, they will also set themselves in the POST array even if they are empty. Each item in the POST will be reflected in the count() of the POST array var. To see if the POST array vars are empty, you need to walk the array (at least I thought so).pilaftank wrote:Is there an easy way in PHP to determine if all the values of a string array are empty? More specifically, how can you detect if there is no form data in the $_POST array.
Code: Select all
$hasEmpty = (count($_POST) != count(array_filter($_POST,'strlen')));Sorry, I missed the array_unique. I agree with you on using array_filter().jshpro2 wrote:Evarah, I think you missed that we were using array_unique on the array before the code, if after array_unique there is more then 1 item then all the $_POST cannot be empty. Anyways I like the array_filter() approach and that is probably the most efficient way as its built in