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...
}
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!";
?>
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.
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
0 Comments