Category: PHP array functions

PHP count() Function Example

PHP Script:


<?php
$lang_arr = array("PHP","JAVASCRIPT","HTML","CSS");
echo count($lang_arr);

$lang_multi_arr = array(
"server_site"=>array("PHP","JSP","ASP"),
"client_site"=>array("JAVASCRIPT","HTML","CSS")
);
echo "<br>";
echo count($lang_multi_arr);
echo "<br>";
echo count($lang_multi_arr,1);
?>

Output:


4
2
8

Description:

PHP count() function count the number of elements in an array and return the count.

PHP count function parameters:

1- Array
2- Boolean [0 or 1]:
By default value is 0.
If value is 0 count function not counts all elements of multidimensional array.
If value is 1 count function counts all elements of multidimensional array.
You can see the above example script having default (0) and 1 as a 2nd parameter and the respective output against these parameters.

PHP in_array() Function Example

PHP Script:


<?php
$os = array("Android", "Windows", "Unix", "Linux");
if (in_array("Unix", $os)) {
echo "Unix found<br>";
}

$a = array( array(‘p’, ‘r’), array(‘p’, ‘h’), ‘o’ );
if ( in_array(array(‘p’, ‘h’), $a )) {
echo “p,h found”;
}
?>

Output:


Unix found
p,h found

Description:

PHP in_array() function checks if a given value exists in an array and
returns TRUE on success, or FALSE on failure.

PHP in_array function parameters:

1- mixed: string/array. The item to be searched in the array.
2- array: The array to be searched.
3- Boolean: If this parameter is set to TRUE then this function will also check the type of searched item in the array to be searched