PHP Classes
elePHPant
Icontem

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

Recommend this page to a friend!
  Info   View files Documentation   View files View files (2)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2018-04-13 (2 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 89 This week: 1All time: 9,022 This week: 405Up
Version License PHP version Categories
countval 1.0.0The 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
Nominee
Vote
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.

Manuel Lemos
  Performance   Level  
Name: zinsou A.A.E.Mo´se is available for providing paid consulting. Contact zinsou A.A.E.Mo´se .
Classes: 40 packages by
Country: Benin Benin
Age: 28
All time rank: 11221 in Benin Benin
Week rank: 1 Up
Innovation award
Innovation award
Nominee: 14x

Winner: 2x

Details
Everybody knows the PHP array_count_values function .It is a very useful function but in some
cases  it may be helpless.
The reason is simple:
The PHP array type can contain any type of data ,absolutely all types.So when you try to use 
array_count_values on an array which contains a type different of numeric type and string type
the result is not exactly what one expect.The function will trigger error for the bad types 
simply because an array can't get all types as offset.And the result of the array_count_values
is justly  an array.

The CountVal class use another approach to count  any type of values .Moreover it can count values in
any type specially in objects which implements the Traversable interface and in arrays.

As an example is always better than a speech :

$array = array(1, "hello", 1, "world", "hello",[1,2,3],[1,2,3],false,0,true,$x=xdict(),&$x,&$x,null,null);

print_r(count_values($array));

the result of this snippet is:
Array
(
    [0] => Array 
        (
            [0] => 1
            [1] => 2
        )

    [1] => Array
        (
            [0] => hello
            [1] => 2
        )

    [2] => Array
        (
            [0] => world
            [1] => 1
        )

    [3] => Array
        (
            [0] => Array
                (
                    [0] => 1
                    [1] => 2
                    [2] => 3
                )

            [1] => 2
        )

    [4] => Array
        (
            [0] =>  //nothing is printed on the screen but if you test it you will got the boolean false...
            [1] => 1
        )

    [5] => Array
        (
            [0] => 0
            [1] => 1
        )

    [6] => Array
        (
            [0] => 1 //1 is printed on the screen but if you test it you will got the boolean true...
            [1] => 1
        )

    [7] => Array
        (
            [0] => xDict Object
                (
                )

            [1] => 3
        )
		
	[8] => Array
        (
            [0] =>   //nothing is printed on the screen but if you test it you will got  null...
            [1] => 2
        )

)


As you can see the approach is really different.But it works fine.

var_dump(count_diff_values($array),value_count($x,$array));
the result based on the snippet above is  :
int(9)
int(3)

You can of course use The object oriented style as
countVal::count_values(),countVal::count_diff_values(),countVal::value_count().

For more information contact me at leizmo@gmail.com or use the forum and don't forget to rate 
the package.
  Files folder image Files  
File Role Description
Plain text file countVal.class.php Class class source
Accessible without login Plain text file readme.txt Doc. readme

 Version Control Unique User Downloads Download Rankings  
 0%
Total:89
This week:1
All time:9,022
This week:405Up