Counting Array Elements : count « Data Structure « PHP





Counting Array Elements

 
<?php

  $dogs = array('A' => 'AA', 'Bud' => 'BB', 'C' => 'D');

  $birds = array('parrot', 'magpie', 'lorakeet', 'cuckoo');

  printf("<p>There are %d dogs and %d birds.</p>", count($dogs), count($birds));

  $birds[] = 'ibis';
  printf("<p>There are %d birds:</p>", count($birds));
  printf("<pre>%s</pre>\n", var_export($birds, TRUE));

  $birds[10] = 'heron';
  unset($birds[3]);
  printf("<p>There are %d birds:</p>", count($birds));
  printf("<pre>%s</pre>\n", var_export($birds, TRUE));
?>
  
  










Related examples in the same category

1.Get the size of an array in PHP using the count() function
2.Getting the Number of Lines in a File
3.Using count() to Iterate Through an Array