countCOUNT all elements in an
ARRAY or something in an
OBJECT.
This function is an alias of
sizeof.
OBJECTS will be studied in more detail ...... in a special chapter of this tutorial
sizeof
COUNT all elements in an ARRAY or something in an OBJECT.
This function is an alias of count.
<?php
int count ( mix $array_or_countable [,
int $mode = COUNT_NORMAL ] )
or
int sizeof ( mix $array_or_countable [,
int $mode = COUNT_NORMAL ] )
where,
$array_or_countable = ARRAY or contable object
$mode = To recursive count or not
( SEE the below TABLE )
?>
$array_or_countable
ARRAY or contable object.
$mode
CONSTANT |
VALUE |
OBSERVATIONS |
COUNT_NORMAL |
0 |
Not recursively count the ARRAY. To count the elements of an unidimensional ARRAY. |
COUNT_RECURSIVE |
1 |
Recursively count the ARRAY. This is particularly useful for counting all the elements of a multidimensional ARRAY. |
ed48 |
COMMON UNIDIMENSIONAL ARRAY
KEY TYPE |
FIRST |
OTHER |
INTEGER |
ALWAYS ZERO |
POSITIVE AND SEQUENTIAL |
KEYS NOT PROVIDED |
ed48 |
VALUE TYPES |
FIRST |
OTHER |
STRING |
ANY OF THE THREE TYPES |
ANY OF THE THREE TYPES |
INTEGER |
FLOAT |
ed48 |
EXERCISE
<?php
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SOME USER-DEFINED ARRAY EXAMPLES
COMMON UNIDIMENSIONAL
$arr_O = array ( mix $var1, mix $var2, ... mix $varN );
$arr_N = [ mix $var1, mix $var2, ... mix $varN ];
The generic element or term of
this type of ARRAY will be given by:
$arr_O['index'] = $arr_N['index']
where index is the position of the term.
(*) Remember that in this case the index of the
first term is always ZERO and the others positive
sequentially represented.
To display the value of any element,
in this type of ARRAY,
we must remember that its indices are sequences,
starting at ZERO and
ending at the value set by count minus 1.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
$arr01 = [ 2, 8, 18, 32, 32, 18, 8 ];
$ct01 = count($arr01);
echo '<br>$arr01<br>NUMBER OF ELEMENTS = ' . $ct01 .
'<br><br>FIRST = ' . $arr01[0];
echo '<br>LAST = ' . $arr01[$ct01 - 1];
$arr02 = [ 2, 8, '18', 32, 'THIRTY-TWO', "eighteen", 8 ];
$ct02 = count($arr02);
echo '<br><br>$arr02<br>NUMBER OF ELEMENTS = ' . $ct02 .
'<br><br>FIRST = ' . $arr02[0];
echo '<br>THIRD = ' . $arr02[2];
$arr03 = [ 299792458, 6.67428E-11, 'c', "G" ];
$ct03 = count($arr03);
echo '<br><br>$arr03<br>NUMBER OF ELEMENTS = ' . $ct03 .
'<br><br>FIRST = ' . $arr03[0];
echo '<br>LAST = ' . $arr03[$ct03 - 1];
$arr04 = [01023, 0B0011, 0x20b ];
$ct04 = count($arr04);
echo '<br><br>$arr04<br>NUMBER OF ELEMENTS = ' . $ct04 .
'<br><br>SECOND = ' . $arr04[1];
echo '<br>LAST = ' . $arr04[$ct04 - 1];
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
?>
RESULT
- - - - - - - - - - - - - - - - - -
$arr01
NUMBER OF ELEMENTS = 7
FIRST = 2
LAST = 8
- - - - - - - - - - - - - - - - - -
$arr02
NUMBER OF ELEMENTS = 7
FIRST = 2
THIRD = 18
- - - - - - - - - - - - - - - - - -
$arr03
NUMBER OF ELEMENTS = 4
FIRST = 299792458
LAST = G
- - - - - - - - - - - - - - - - - -
$arr04
NUMBER OF ELEMENTS = 3
SECOND = 3
LAST = 523
- - - - - - - - - - - - - - - - - -
UNIDIMENSIONAL ASSOCIATIVE ARRAY
KEY TYPES |
FIRST |
OTHER |
STRING |
SINCE IS CONSIDERED VALID |
SINCE IS CONSIDERED VALID AND NOT REPEATED |
INTEGER |
NOT NECESSARILY EQUIVALENT TO ZERO |
DO NOT NEED TO BE SEQUENTIAL, ONLY DIFFERENTS |
ONLY ONE KEY TYPE IS ACCEPTED |
ed48 |
VALUE TYPES |
FIRST |
OTHER |
STRING |
ANY OF THE THREE TYPES |
ANY OF THE THREE TYPES |
INTEGER |
FLOAT |
ed48 |
EXERCISE
<?php
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SOME USER-DEFINED ARRAY EXAMPLES
UNIDIMENSIONAL ASSOCIATIVE
$arr_O = array( mix $key1 => mix $var1,
mix $key2 => mix $var2,
. . . . . . . . . . . . . . . . . . . . .
mix $keyN => mix $varN );
$arr_N = [ mix $key1 => mix $var1,
mix $key2 => mix $var2,
. . . . . . . . . . . . . . . . . . . . .
mix $keyN => mix $varN ];
The generic element or ELEMENT of
this type of ARRAY will be given by:
$arr_O['index'] = $arr_N['index'].
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
$arr05 = array('K' => 2, "L" => 8, "M" => 18,
"N" => 32, "O" => 32, "P" => 18, "Q" => 8);
$arr05 = [ 'K' => 2, "L" => 8, "M" => 18,
"N" => 32, "O" => 32, "P" => 18, "Q" => 8 ];
$ct05n = count($arr05, COUNT_NORMAL);
$ct05r = count($arr05, COUNT_RECURSIVE);
echo '<br>$arr05 has ' . $ct05n .
' elements - COUNT_NORMAL<br>';
echo '$arr05 has ' . $ct05r .
' elements - COUNT_RECURSIVE<br><br>';
echo 'FIRST = ' . $arr05['K'];
echo '<br>LAST = ' . $arr05['Q'];
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
$arr05:
first ELEMENT has index LITERAL =
= 'K' and value = 2
and the
last ELEMENT has index LITERAL =
= 'Q' and value = 8
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
$arr06 = [ 'THIRTY-TWO' => 32, "EIGHTEEN" => 18 ];
$ct06n = count($arr06, COUNT_NORMAL);
$ct06r = count($arr06, COUNT_RECURSIVE);
echo '<br><br>$arr06 has ' . $ct06n .
' elements - COUNT_NORMAL<br>';
echo '$arr06 has ' . $ct06r .
' elements - COUNT_RECURSIVE<br><br>';
echo 'FIRST = ' . $arr06['THIRTY-TWO'];
echo '<br>LAST = ' . $arr06['EIGHTEEN'];
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
$arr06:
first ELEMENT has index LITERAL =
= 'THIRTY-TWO' and value = 32
and the
last ELEMENT has index LITERAL =
= 'EIGHTEEN' and value = 18
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
$arr07 = [ 'c' => 299792458, 'G' => 6.67428E-11 ];
$ct07n = count($arr07, COUNT_NORMAL);
$ct07r = count($arr07, COUNT_RECURSIVE);
echo '<br><br>$arr07 has ' . $ct07n .
' elements - COUNT_NORMAL<br>';
echo '$arr07 has ' . $ct07r .
' elements - COUNT_RECURSIVE<br><br>';
echo 'FIRST = ' . $arr07['c'];
echo '<br>LAST = ' . $arr07['G'];
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
$arr07:
first ELEMENT has index LITERAL =
= 'c' and value = 299792458
and the
last ELEMENT has index LITERAL =
= 'G' and value = 6.67428E-11
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
$arr08 = [ 2 => 'two', 4 => 'four' ];
$ct08n = count($arr08, COUNT_NORMAL);
$ct08r = count($arr08, COUNT_RECURSIVE);
echo '<br><br>$arr08 has ' . $ct08n .
' elements - COUNT_NORMAL<br>';
echo '$arr08 has ' . $ct08r .
' elements - COUNT_RECURSIVE<br><br>';
echo 'FIRST = ' . $arr08[2];
echo '<br>LAST = ' . $arr08[4];
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
?>
RESULT
- - - - - - - - - - - - - - - - - -
$arr05 has 7 elements -> COUNT_NORMAL
$arr05 has 7 elements -> COUNT_RECURSIVE
FIRST = 2
LAST = 8
- - - - - - - - - - - - - - - - - -
$arr06 has 2 elements -> COUNT_NORMAL
$arr06 has 2 elements -> COUNT_RECURSIVE
FIRST = 32
LAST = 18
- - - - - - - - - - - - - - - - - -
$arr07 has 2 elements -> COUNT_NORMAL
$arr07 has 2 elements -> COUNT_RECURSIVE
FIRST = 299792458
LAST = G
- - - - - - - - - - - - - - - - - -
$arr08 has 2 elements -> COUNT_NORMAL
$arr08 has 2 elements -> COUNT_RECURSIVE
FIRST = two
LAST = four
- - - - - - - - - - - - - - - - - -
COMMON MULTIDIMENSIONAL ARRAY
KEY TYPES |
FIRST |
OTHER |
INTEGER |
ALWAYS ZERO |
POSITIVE AND SEQUENTIAL |
ed48 |
VALUE TYPES |
FIRST |
OTHER |
STRING |
ANY OF THE FOUR TYPES |
ANY OF THE FOUR TYPES |
INTEGER |
FLOAT |
ARRAY |
AT LEAST ONE MUST HAVE THIS VALUE TYPE |
ed48 |
EXERCISE
<?php
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SOME USER-DEFINED ARRAY EXAMPLES
COMMON MULTIDIMENSIONAL
$arr_O = array( mix $var1, mix $var2, ... mix $varN );
$arr_N = [ mix $var1, mix $var2, ... mix $varN ];
The generic element or term of this type of ARRAY
may vary depending on the amount
of ARRAYS involved in its constitution.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
$arr09 = array(2, 8, 18, 32, array (32, 18, 8));
$arr09 = [ 2, 8, 18, 32, [ 32, 18, 8 ] ];
$ct09n = count($arr09);
$ct09r = count($arr09, COUNT_RECURSIVE);
echo '<br><br>$arr09 has ' . $ct09n .
' elements - COUNT_NORMAL<br>';
echo '$arr09 has ' . $ct09r .
' elements - COUNT_RECURSIVE<br><br>';
echo 'FIRST = ' . $arr09[0];
echo '<br>SECOND = ' . $arr09[1];
echo '<br>THIRD = ' . $arr09[2];
echo '<br>FOURTH = ' . $arr09[3];
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you try to get the result of the term FOURTH,
using echo or print,
surely PHP will issue an error warning.
echo '<br>FIFTH TERM ($arr09) = ' . $arr09[4];
Notice: Array to string conversion in
H:\...\rsr\echo01.php on line 22
This occurs because the FOURTH term is an ARRAY.
In this case, the generic term should contain index 4,
(of the general ordering),
plus the relative indexes of the terms of
the ARRAY which complements it, ie:
$arr0[4][0], $arr0[4][1] and $arr0[4][2]
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
echo '<br>"FIFTH" = ' . $arr09[4][0];
echo '<br>"SIXTH" = ' . $arr09[4][1];
echo '<br>"SEVENTH" = ' . $arr09[4][2];
// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
$arr12 = [ ['01023', '0B0011', '0x20b'],
['octal', 'binary', 'hexadecimal' ] ];
$ccr12n = count($arr12);
$ccr12r = count($arr12, COUNT_RECURSIVE);
echo '<br><br>$arr12 has ' . $ccr12n .
' elements - COUNT_NORMAL<br>';
echo '$arr12 has ' . $ccr12r .
' elements - COUNT_RECURSIVE<br><br>';
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This ARRAY has, generically,
TWO TERMS, and both are ARRAY
This ARRAY has TWO TERMS, and both are ARRAY,
this leads to the impossibility of
using echo or print to print them directly.
1 . If we had only the FIRST ARRAY,
defined by $arr12, we would have:
$arr12[0] as the FIRST TERM,
$arr12[1] as the SECOND TERM and
$arr12[2] as the THIRD.
2 . If we had only the SECOND ARRAY,
defined by $arr12, we would have:
$arr12[0] as the FIRST TERM,
$arr12[1] as the SECOND TERM and
$arr12[2] as the THIRD.
3 . However, our ARRAY is made up of TWO ARRAYS.
The individual TERMS of this ARRAY will be obtained
by combining the indexes of the TWO ARRAYS:
arr12[0][0], arr12[0][1] and arr12[0][2]
arr12[1][0], arr12[1][1] and arr12[1][2]
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
echo $arr12[0][0] . ' - ' . $arr12[1][0];
echo '<br>' . $arr12[0][1] . ' - ' . $arr12[1][1];
echo '<br>' . $arr12[0][2] . ' - ' . $arr12[1][2];
$arr13 = ["Countries", "Continents",
["Brasil", "Portugal", "Japan"],
["South America", "Europe", "Asia"] ];
$ct13n = count($arr13);
$ct13r = count($arr13, COUNT_RECURSIVE);
echo '<br><br>$arr13 has ' . $ct13n .
' elements - COUNT_NORMAL<br>';
echo '$arr13 has ' . $ct13r .
' elements - COUNT_RECURSIVE<br><br>';
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This ARRAY has, generically,
FOUR TERMS: TWO COMMONS and TWO ARRAYS
This leads to the impossibility of
using echo or print to print them directly.
1 . If we had only COMMON TERMS we would have:
$arr13[0] as the FIRST TERM,
$arr13[1] as the SECOND TERM,
$arr13[2] as the THIRD and
$arr13[3] as the FOURTH.
2 . To relate
"Countries" with ["Brasil", "Portugal", "Japan"]
let's reference:
$arr13[2][0] for "Brasil";
$arr13[2][1] for "Portugal";
and
$arr13[2][2] for "Japan".
3 . To relate
"Continents" with ["South America", "Europe", "Asia"]
let's reference:
$arr13[3][0] for "South America";
$arr13[3][1] for "Europe";
and
$arr13[3][2] for "Asia".
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
echo $arr13[0] . ' -> ( ' . $arr13[2][0] . ' - ' .
$arr13[2][1] . ' - ' . $arr13[2][2] . ' )';
echo '<br><br>' . $arr13[1] . ' -> ( ' . $arr13[3][0] .
' - ' .
$arr13[3][1] . ' - ' . $arr13[3][2] . ' )';
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
?>
RESULT
- - - - - - - - - - - - - - - - - -
$arr09 has 5 elements -> COUNT_NORMAL
$arr09 has 8 elements -> COUNT_RECURSIVE
FIRST = 2
SECOND = 8
THIRD = 18
FOURTH = 32
"FIFTH" = 32
"SIXTH" = 18
"SEVENTH" = 8
- - - - - - - - - - - - - - - - - -
$arr12 has 2 elements -> COUNT_NORMAL
$arr12 has 8 elements -> COUNT_RECURSIVE
01023 - octal
0B0011 - binary
0x20b - hexadecimal
- - - - - - - - - - - - - - - - - -
$arr13 has 4 elements -> COUNT_NORMAL
$arr13 has 10 elements -> COUNT_RECURSIVE
Countries -> ( Brasil - Portugal - Japan )
Continents -> ( South America - Europe - Asia )
- - - - - - - - - - - - - - - - - -
ASSOCIATIVE MULTIDIMENSIONAL ARRAY
KEY TYPES |
FIRST |
OTHER |
STRING |
SINCE IS CONSIDERED VALID |
SINCE IS CONSIDERED VALID AND NOT REPEATED |
INTEGER |
NOT NECESSARILY EQUIVALENT TO ZERO |
DO NOT NEED TO BE SEQUENTIAL, ONLY DIFFERENTS |
BOTH KEY TYPES ARE ACCEPTED |
ed48 |
VALUE TYPES |
FIRST |
OTHER |
STRING |
ANY OF THE FOUR TYPES |
ANY OF THE FOUR TYPES |
INTEGER |
FLOAT |
ARRAY |
AT LEAST ONE MUST HAVE THIS VALUE TYPE |
ed48 |
EXERCISE
<?php
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Although print_r,
because of bool $return = TRUE, (true),
does not display any data,
this data should be
considered as exhibitables
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
$arr16 = [ "Countries" => [ "Brasil", "Portugal", "Japan" ],
"Continents" => ["South America", "Europe", "Asia"]];
echo '<pre>';
print_r($arr16, TRUE);
echo '</pre>';
$ccr16n = count($arr16);
$ccr16r = count($arr16, COUNT_RECURSIVE);
echo '<br><br>$arr16 has ' . $ccr16n .
' elements - COUNT_NORMAL<br>';
echo '$arr16 has ' . $ccr16r .
' elements - COUNT_RECURSIVE<br><br>';
echo 'Countries => { ' . $arr16['Countries'][0] . ', ' .
$arr16['Countries'][1] . ', ' .
$arr16['Countries'][2] . ' }';
echo '<br>Continents => { ' . $arr16['Continents'][0] . ', ' .
$arr16['Continents'][1] . ', ' .
$arr16['Continents'][2] . ' }';
$arr17 =
[ "Countries" => [ 'BRA' => "Brasil",
'POR' => "Portugal",
'JPN' => "Japan" ],
"Continents" => ['SA' => "South America",
'EU' => "Europe",
'AS' => "Asia"]];
echo '<pre>';
print_r($arr17, true);
echo '</pre>';
$ct17n = count($arr17);
$ct17r = count($arr17, COUNT_RECURSIVE);
echo '<br><br>$arr17 has ' . $ct17n .
' elements - COUNT_NORMAL<br>';
echo '$arr17 has ' . $ct17r .
' elements - COUNT_RECURSIVE<br><br>';
echo 'Countries => { ' . $arr17['Countries']['BRA'] . ', ' .
$arr17['Countries']['POR'] . ', ' .
$arr17['Countries']['JPN'] . ' }';
echo '<br>Continents => { ' . $arr17['Continents']['SA'] . ', ' .
$arr17['Continents']['EU'] . ', ' .
$arr17['Continents']['AS'] . ' }';
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
?>
RESULT
- - - - - - - - - - - - - - - - - - - - - - -
$arr16 has 2 elements -> COUNT_NORMAL
$arr16 has 8 elements -> COUNT_RECURSIVE
Countries => { Brasil, Portugal, Japan }
Continents => { South America, Europe, Asia }
- - - - - - - - - - - - - - - - - - - - - - -
$arr17 has 2 elements -> COUNT_NORMAL
$arr17 has 8 elements -> COUNT_RECURSIVE
Countries => { Brasil, Portugal, Japan }
Continents => { South America, Europe, Asia }
- - - - - - - - - - - - - - - - - - - - - - -
EXERCISE
<?php
$var05a = 'Your Wildest Dreams';
$nbvr = count($var05a);
echo $nbvr . '<br><br>';
?>
RESULT
Warning: count(): Parameter must be an array or an object that implements Countable in H:\code\count05.php on line 5
1