0

I have a form that sends integers up to $_POST['ore'] from a table of select boxes and text input fields. Array in question is:

print_r($_POST)
//returns
Array
(
[ore] => Array
    (
        [0] => 1
        [1] => 2
        [2] => 0
        [3] => 0
        [4] => 0
        [5] => 0
        [6] => 0
        [7] => 0
        [8] => 0
        [9] => 0
    )
....

And that's all the bigger it gets, it's a small application. 10 select boxes, 10 input boxes. The form itself and the other functions work great.

However, On submit the data is sent to another page that processes the information gathered and generates a new table with a while loop based on the count of that array. But, I only want to count the values that are greater than 0 as zero is technically empty.

I have tried multiple functions, examples, bits pieces and tried dumb luck too but no positive results. I have gotten some to work partially, but most of the time they leave out the last row.

I currently have size = count($_POST['ore']); - however this creates a table with 10 rows (as it should) and fills the rest of the rows in the table with the last loop of the while code. Functional, but annoying and not what I would like to do.

I have tried array_filter with

function nonzero($var){
   return ($var > 0); }
$size = array_filter($_POST['ore'], "nonzero");

No luck.

I have tried if(isset($_POST['ore'])) in multiple places, but since there is a 0 value in the array, it's technically set.

So, how do I count ,or filter then count, the $_POST['ore'] array values to strip away the 0's and only count or return the keys that are greater than 0?

My beat up keyboard and I thank you in advance.


Edit - Found the answer thank you all for the feedback this early in the morning

function nonzero($var){
   return ($var > 0); }
$size = count(array_filter($_POST['ore'], "nonzero"));

^^ that did it

1
  • Please edit your question with a meaningful title. Commented Sep 19, 2012 at 10:51

4 Answers 4

1

You have tried:

$size = array_filter($_POST['ore], "nonzero");

Is that a typo when making this post or did you copy that from the code? If so, an apostrophe is missing. Also, $size will be the returned array with all the values that were greater than 0, so for the size you need:

$size = count(array_filter($_POST['ore'], "nonzero"));
Sign up to request clarification or add additional context in comments.

2 Comments

typo from the post, I'll fix that
I just updated this post, what was the problem you were having with the array_filter? What I just posted should work.
1

You do not actually need to define a filter function if there are no negative elements in the array, array_filter filters out zeroes by default:

$size = count(array_filter($_POST['ore']));

1 Comment

I'll try that too, and no, all positive elements.
0

Your function should work properly, the only thing that you must to know is that the function is returning you an array so:

$_POST['ore'][] = 1;
$_POST['ore'][] = 2;
$_POST['ore'][] = 0;
$_POST['ore'][] = 0;

function nonzero($var){ 
    return ($var > 0); } 

$arr = array_filter($_POST['ore'], "nonzero"); 
echo count($arr); // this returns 2

Comments

0

Hello Just try to unset the indexes of an array which have 0 as value

foreach($array as $array_key=>$array_item)
{
  if($array[$array_key] == 0)
  {
    unset($array[$array_key]);
  }

}

Hope this will help you

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.