Count array key values with array_count_values, count and sizeof PHP functions

sizeof

According to the php.net, sizeof() is an alias of count()

It count all the elements in an array.

One more important thing is:

If your array is "huge"

It is reccomended to set a variable first for this case:

THIS->
$max = sizeof($huge_array);
for($i = 0; $i < $max;$i++)
{
code...
}

IS QUICKER THEN->
for($i = 0; $i < sizeof($huge_array);$i++)
{
code...
}

count

It Count all elements in an array.

Syntax:

int count ( mixed $array_or_countable [, int $mode = COUNT_NORMAL ] )

Parameters:

array_or_countable
An array or Countable object.
mode
If the optional mode parameter is set to COUNT_RECURSIVE (or 1), count() will recursively count the array. This is particularly useful for counting all the elements of a multidimensional array.

Return Values:
It will Returns the number of elements in an array_or_countable. If the parameter is not an array or not an object with implemented Countable interface, 1 will be returned. There is one exception, if array_or_countable is NULL, 0 will be returned.

<?php
$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
$result = count($a);
// $result == 3
echo $result;

$b[0] = 7;
$b[5] = 9;
$b[10] = 11;
$result = count($b);
// $result == 3
echo $result;
$result = count(null);
// $result == 0
echo $result;
$result = count(false);
// $result == 1
echo $result;
$food = array(
              'fruits' => array(
                                'orange',
                                'banana',
                                'apple'
                                ),
              'veggie' => array('carrot',
                                'collard',
                                'pea'
                                )
);
// recursive count
echo count($food, COUNT_RECURSIVE); // output 8
or
echo count($food, 1); // output 8

// normal count
echo count($food); // output 2
// count only multidimensional array value
echo (count($food,COUNT_RECURSIVE)-count($food,0)); //output 6

$MyArray = array (
                  array(1,
                        2,
                        3
                        ),
                   1,
                   'a',
                   array(
                         'a',
                         'b',
                         'c',
                         'd'
                         )
                 );

$result = count($MyArray,0); // output 4
$result = count($MyArray,1); // output 11

// Both level values, but only values
echo(array_sum(array_map('count',$MyArray ))); //output 9 (9 values)

Explaination: array_map() returns an array containing all the elements of array 
after applying the callback function to each one.

in our case, callback function is "count" which will count all the values.
print_r(array_map('count',$MyArray ));

Will give below output:

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

And array_sum which calculates the sum of all the values in an array.

So (array_sum(array_map('count',$MyArray ))) wiil sum 3 + 1 + 1 + 4

// Only second level values
echo (count($MyArray ,COUNT_RECURSIVE)-count($MyArray )); 
//output 7 ((all elements) - (first elements))
?>

There is a simple script with example for counting rows and columns
of a two-dimensional array.

<?php
$cars = array
(
"first" => array(
                 "carname" => "Volvo",
                 "carprice" => 22,
                 "caraverage" => 60,
                 "caroil" => 'castol',
                 "caryear" => 2012
),
"second" => array(
                  "carname" => "BMW",
                  "carprice" => 15,
                  "caraverage" => 70,
                  "caroil" => 'hp',
                  "caryear" => 2013
),
"third" => array(
                 "carname" => "Saab",
                 "carprice" => 5,
                 "caraverage" => 80,
                 "caroil" => 'mobil',
                 "caryear" => 2014
),
"fourth" => array(
                  "carname" => "Land Rover",
                  "carprice" => 17,
                  "caraverage" => 90,
                  "caroil" => 'AMSoil',
                  "caryear" => 2015
)
);

$rows = count($cars,0); // output 4
$cols = (count($cars,1)/count($cars,0))-1; // output 24/4 = 6 -1 = 5
print "There are {$rows} rows and {$cols} columns in the table!";
?>

array_count_values

array_count_values — Counts all the values of an array

Syntax:

array array_count_values ( array $array )

Returns an associative array of values from array as keys and their count as value.

<?php

$array = array(1, "hello", 1, "world", "hello");

echo "<pre>";

print_r(array_count_values($array));

echo "</pre>";

?>



// Output

Array

(

[1] => 2

[hello] => 2

[world] => 1

)


One Interesting PHP function for counting all the values of an array with case-insensitive matching,

<?php

function array_icount_values($array) {

    $ret_array = array();

    foreach($array as $value) {

        foreach($ret_array as $key2 => $value2) {

            if(strtolower($key2) == strtolower($value)) {

                $ret_array[$key2]++;

                continue 2;

            }

        }

        $ret_array[$value] = 1;

    }

    return $ret_array;

}



$ar = array(

            'J. Karjalainen',

            'J. Karjalainen',

            60,

            '60',

            'J. Karjalainen',

            'j. karjalainen',

            'Fastway',

            'FASTWAY',

            'Fastway',

            'fastway',

            'YUP'

            );

$ar2 = array_count_values($ar); // Normal matching

$ar = array_icount_values($ar); // Case-insensitive matching

echo "<pre>";

print_r($ar2);

echo "</pre>";

echo "<pre>";

print_r($ar);

echo "</pre>";

?>



// Output with Normal matching

Array

(

    [J. Karjalainen] => 3

    [60] => 2

    [j. karjalainen] => 1

    [Fastway] => 2

    [FASTWAY] => 1

    [fastway] => 1

    [YUP] => 1

)

// Output with Case-insensitive matching

Array

(

    [J. Karjalainen] => 4

    [60] => 2

    [Fastway] => 4

    [YUP] => 1

)


And one line PHP script for counting all the values of an array with case-insensitive matching,

<?php

$ar = array(

            'J. Karjalainen',

            'J. Karjalainen',

            60,

            '60',

            'J. Karjalainen',

            'j. karjalainen',

            'Fastway',

            'FASTWAY',

            'Fastway',

            'fastway',

            'YUP'

            );

$ar = array_count_values(array_map('strtolower', $ar));



echo "<pre>";

print_r($ar);

echo "</pre>";

?>



//Output:

Array

(

    [j. karjalainen] => 4

    [60] => 2

    [fastway] => 4

    [yup] => 1

)

Post a Comment

0 Comments