0

I have this array:

$auflistung = array ($_POST['jacken'], $_POST['hosen'], $_POST['oberteil'], $_POST['tasche'], $_POST['schuhe'], $_POST['accessoireschmuck1'], $_POST['accessoireschmuck2'], $_POST['accessoireschmuck3'], $_POST['accessoireschmuck4'], $_POST['accessoireschmuck5'], $_POST['accessoireschmuck6']);

It contains e.g.:

array(11) {
  [0]=>      string(10) "1288023365"
  [1]=>      string(10) "1246154493"
  [2]=>      string(0) ""
  [3]=>      string(10) "1293143681"
  [4]=>      string(10) "1293143242"
  [5]=>      string(0) ""
  [6]=>      string(0) ""
  [7]=>      string(0) ""
  [8]=>      string(0) ""
  [9]=>      string(0) ""
  [10]=>     string(0) ""
}

Now i need the amount of aviable values. In this case 4

I tried: echo count( $auflistung ); but the result includes allso the empty values, so 11. Any ideas?

2
  • 2
    Why not fix the origin of the empty elements and create the array with only non empty values? Commented Oct 31, 2013 at 0:25
  • @ZombieHunter has a good point. I've extended my answer. Commented Oct 31, 2013 at 0:37

1 Answer 1

4

Use array_filter to iterate over each value. If you don't provide a callback, any key whose value == false gets removed. Empty is close enough to false for PHP.

echo count(array_filter($auflistung));

ZombieHunter has a good point about creating the array from non-empty values. This is a clean way of doing that.

$keys=array(
    'jacken', 
    'hosen', 
    'oberteil', 
    'tasche', 
    'schuhe', 
    'accessoireschmuck1', 
    'accessoireschmuck2', 
    'accessoireschmuck3', 
    'accessoireschmuck4', 
    'accessoireschmuck5', 
);

$auflistung=array();
foreach ($keys as $key){
    if (isset($_POST[$key]) && !empty($_POST[$key])) $auflistung[]=$_POST[$key];
}

echo count($auflistung);
Sign up to request clarification or add additional context in comments.

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.