PHP Classes

PHP Count Array by Value Type: Get the number of values in array of a given type

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 177 All time: 8,749 This week: 206Up
Version License PHP version Categories
countval 2.0.1The PHP License5PHP 5, Data types
Description 

Author

This class can get the number of values in array of a given type.

It can traverse a given array and count the number of values like the array_count_values function but it returns only those that have a given type.

The class can also count values of a type in variables objects of classes that act like arrays and implement the Traversable interface.

Innovation Award
PHP Programming Innovation award nominee
May 2018
Number 7
Array entries can have values of many types. PHP can return the count of entries in an array but if you just want to get the count of elements of a given type, PHP does not provide a direct means to achieve that.

This class not only can count the elements of a given type, but it also can handle nested arrays.

Manuel Lemos
Picture of zinsou A.A.E.Moïse
  Performance   Level  
Name: zinsou A.A.E.Moïse is available for providing paid consulting. Contact zinsou A.A.E.Moïse .
Classes: 50 packages by
Country: Benin Benin
Age: 34
All time rank: 6761 in Benin Benin
Week rank: 180 Up1 in Benin Benin Equal
Innovation award
Innovation award
Nominee: 23x

Winner: 2x

Documentation

AVC

Build Status Scrutinizer Code Quality Build Status Code Intelligence Status

Extends PHP array_count_values native function to count any PHP type value in iterable variables

Requires: PHP 5.3+

Why use this instead of native PHP array_count_values function?

Typically you would use it:

  1. You need to count values not only in arrays but also in iterator ,generator.
  2. You want to count values of other types than integer and string. .

How to use it

Require the library by issuing this command:

composer require manuwhat/avc

Add require 'vendor/autoload.php'; to the top of your script.

require 'AVC.php';//require helpers file

var_dump(count_values(array_merge(range(0, 5), array(array()), array(array()), array(array(6)))));

/* output
array(8) {
  [0]=>
  array(2) {
    [0]=>
    int(0)
    [1]=>
    int(1)
  }
  [1]=>
  array(2) {
    [0]=>
    int(1)
    [1]=>
    int(1)
  }
  [2]=>
  array(2) {
    [0]=>
    int(2)
    [1]=>
    int(1)
  }
  [3]=>
  array(2) {
    [0]=>
    int(3)
    [1]=>
    int(1)
  }
  [4]=>
  array(2) {
    [0]=>
    int(4)
    [1]=>
    int(1)
  }
  [5]=>
  array(2) {
    [0]=>
    int(5)
    [1]=>
    int(1)
  }
  [6]=>
  array(2) {
    [0]=>
    array(0) {
    }
    [1]=>
    int(2)
  }
  [7]=>
  array(2) {
    [0]=>
    array(1) {
      [0]=>
      int(6)
    }
    [1]=>
    int(1)
  }
}

*/

AS you may see the result is a multi-dimensional array and this is great but not very convenient. So the package provide an iterator to easily handle the result.One can use it this way:

require 'AVC.php';//require helpers file

$x=count_values(array_merge(range(0, 5), array(array()), array(array()), array(array(6))),true);

foreach ($x as $value=>$count){
	if(is_array($value)||is_object($value)){
		var_dump($value);
	}elseif(is_resource($value)){
		var_dump(stream_get_meta_data ($value));
	}else{
		echo "$value=>$count<br>";
	}

}

When the handled variable doesn't contain type other than integer or string the function return exactly the same output as the native array_count_values function except if an iterator have been required.

Note that when the handled variable contain types other than integer and string and an iterator has been required as result, you can access the count using array access syntax .For example:

require 'AVC.php';//require helpers file

$x=count_values(array_merge(range(0, 5), array(array()), array(array()), array(array(6))),true);

echo $x[array()];
/*
output:
2
*/

3 helpers are provided in the AVC.php file

count_values,value_count,count_diff_values

To run unit tests

phpunit  ./tests

  Files folder image Files (10)  
File Role Description
Files folder imagesrc (2 files)
Files folder imagetests (1 file)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file AVC.php Aux. Auxiliary script
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file readme.txt Doc. readme

  Files folder image Files (10)  /  src  
File Role Description
  Plain text file AVC.php Class Class source
  Plain text file AVCIterator.php Class Class source

  Files folder image Files (10)  /  tests  
File Role Description
  Plain text file AVCTest.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 90%
Total:177
This week:0
All time:8,749
This week:206Up