PHP array_key_exists() Function Example

PHP Script:


<?php
$sample_arr1 = array("a"=>"Codeigniter","b"=>"CakePHP","c"=>"Yui");
$sample_arr2 = array("Codeigniter","CakePHP","Yui");

if(array_key_exists("a",$sample_arr1))
{
echo "Key exists and contains value: ".$sample_arr1["a"];
}
else
{
echo "Key does not exist.";
}

if(array_key_exists(1,$sample_arr2))
{
echo " <br >Key exists and contains value: ".$sample_arr2[1];
}
else
{
echo "Key does not exist.";
}
?>

Output:


Key exists and contains value: Codeigniter
Key exists and contains value: CakePHP

Description:

PHP array_key_exists() function is used to check an array for a specified key if the key exist then true is return if not exist then false is return.
As shown in above example script key a is exists in array $sample_arr1 so this function return true.
And array $sample_arr2 have no keys so by default integer keys [0,1,2] are generated automatically because array contains 3 elements. And we check key “1” whether it is exist or not so the function returns true because key 1 exists.

PHP array_key_exists() function parameters:

1- Key: Specifies the key for checking whether it is exists or not.
2- Array: The array which is going to be checked against specified key.

PHP array_merge() Function Example

PHP Script:


<?php
// Example 1 [Merging associative arrays. When two or more arrays have same key
// then the last array key value overrides the others one]

$array1 = array("a" => "JAVA", "b" => "ASP");
$array2 = array("c" => "C", "b" => "PHP");
echo " <br> Example 1 Output: <br>";
print_r(array_merge($array1,$array2));

// Example 2 [When you want to merge arrays having integer keys and
//want to reset integer keys to start from 0 then use array_merge() function]

$array3 =array(5 => "CSS",6 => "CSS3");
$array4 =array(8 => "JAVASCRIPT",9 => "HTML");
echo " <br> Example 2 Output: <br>";
print_r(array_merge($array3,$array4));

// Example 3 [When you want to merge arrays having integer keys and
// want to retain integer keys as it is then use PLUS (+) operator to merge arrays]

$array5 =array(5 => "CSS",6 => "CSS3");
$array6 =array(8 => "JAVASCRIPT",9 => "HTML");
echo " <br> Example 3 Output: <br>";
print_r($array5+$array6);

// Example 4 [When single array pass to array_merge having integer keys
// then the array return by array_merge have integer keys starting from 0]

$array7 =array(3 => "CSS",4 => "CSS3");
echo " <br> Example 4 Output: <br>";
print_r(array_merge($array7));
?>

Output:


Example 1 Output:
Array
(
[a] => JAVA
[b] => PHP
[c] => C
)

Example 2 Output:
Array
(
[0] => CSS
[1] => CSS3
[2] => JAVASCRIPT
[3] => HTML
)

Example 3 Output:
Array
(
[5] => CSS
[6] => CSS3
[8] => JAVASCRIPT
[9] => HTML
)

Example 4 Output:
Array
(
[0] => CSS
[1] => CSS3
)

Description:

PHP array_merge() function is used to merge arrays and can also accept a single array and return the resultant merged array.

PHP array_merge() function parameters:

1- Array [1 or more]

PHP array_shift() Function Example

PHP Script:


<?php
$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_shift($stack);
print_r($fruit);
echo "<br />";
print_r($stack);
?>

Output:


orange
Array
(
[0] => banana
[1] => apple
[2] => raspberry
)

Description:

PHP array_shift() function shifts off/pops the first/top value of the array and returns it, reducing the array by one element and moving everything down. The numeric key will be re-assigned starting from zero, while the literal keys would remain unchanged.
This method resets the array pointer of the input array after use.

PHP array_shift function parameters:

1- array: The array to be shifted.

PHP array_shift function output:

1- mixed: Returns the shifted value, or NULL if array is empty or it is not an array, where shifted value can itself be an array.

PHP is_array() Function Example

PHP Script:


<?php
$test_var = "phptutors";
$checking_status = is_array($test_var);
echo $checking_status?"It's an array":"It's not an array";

echo "<br>";

$test_array = array("php","is_array","function");
$checking_status = is_array($test_array);
echo $checking_status?"It's an array":"It's not an array";
?>

Output:


It's not an array
It's an array

Description:

PHP is_array() function checks whether the given variable is an array or not and
return true when the variable is an array or false when the variable is not an array as shown in above example
we use $checking_status in a ternary expression if it is true "It's an array" message print
and if it is false "It's not an array" message print.

PHP is_array function parameters:

1- Variable: Specifies the variable which is going to checked by is_array function.

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 Arrays Example

Definition of Array (computing):

An array is like a container having sections for holding data and each section has a unique identity called index which is used to access an element in that section.

Types of Arrays in PHP:

Indexed arrays: Arrays having numeric indexes.
Associative arrays: Arrays having named keys.
Multidimensional arrays: Arrays with in arrays.

PHP Indexed Array Syntax:


<?php
$php_indexed_array = array("PHP","INDEXED","ARRAY");

//Iterate through an array declared above

for($index = 0; $index < count($php_indexed_array); $index++)
{
echo $index ." => ". $php_indexed_array[$index] ."<br/ >" ;
}
?>

/*** OUTPUT ***/

0 => PHP
1 => INDEXED
2 => ARRAY

PHP Associative Array Syntax:


<?php
$php_assoc_array = array("lang"=>"PHP", "c_type"=>"ASSOCIATIVE", "p_type"=>"ARRAY");

//Iterate through an array declared above

foreach($php_assoc_array as $key => $value)
{
echo $key ." => ". $value ."<br/ >" ;
}
?>

/*** OUTPUT ***/

lang => PHP
c_type => ASSOCIATIVE
p_type => ARRAY

PHP Multidimensional Array Syntax:


<?php
$php_multi_array = array("lang"=>"PHP", "type"=>array("c_type"=>"MULTIDIMENSIONAL", "p_type"=>"ARRAY"));

//Iterate through an array declared above

foreach($php_multi_array as $key => $value)
{
if (!is_array($value))
{
echo $key ." => ". $value ."<br/ >" ;
}
else
{
echo $key ." => array( <br/ >";

foreach ($value as $key2 => $value2)
{
echo $key2 ." => ". $value2 ."<br/ >";
}

echo ")";
}
}
?>

/*** OUTPUT ***/

lang => PHP
type => array(
c_type => MULTIDIMENSIONAL
p_type => ARRAY
)

Note:
1- PHP count() Function is used to count elements in array.
2- PHP is_array() Function is used to check whether a variable is an array or not.

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

PHP md5() Function Example

PHP Script:

<?php
$str=”PHP TUTORS md5 FUNCTION EXAMPLE”;
echo md5($str);
?>

Output:

cb97a255c33139010701c7a68c05d689

Description:

PHP md5() function calculates the MD5 hash of a string.
This function returns the calculated MD5 hash on success, or FALSE on failure.
It can be used to store password in database as encrypted form [md5 hash].

PHP md5 function parameters:

1- String: String for which md5 hash is going to be calculated.
2- Bolean value [optional]:
TRUE is for Raw 16 character binary format.
FALSE is for 32 character hex number.
By default value is FALSE.

PHP html_entity_decode() Function Example

PHP Script:

<?php
$str = “A ‘PHPTUTORS’ is &lt;b&gt;Awsome&lt;/b&gt;”;
echo html_entity_decode($str);
echo “< br/ >”;
echo html_entity_decode($str, ENT_QUOTES);
?>

Output:

A ‘PHPTUTORS’ is Awsome< br/ >A ‘PHPTUTORS’ is Awsome

Description:

html_entity_decode() function Convert all HTML entities to their applicable characters. In above example the source of the output will look like

[

A 'PHPTUTORS' is <b>Awsome</b>< br/ >A 'PHPTUTORS' is <b>Awsome</b>

]

html_entity_decode() function parameters:

1- string : The input string value.
2- int : Bitmask  for specifying criteria of encodings. [Optional] Default value : [ int $flags = ENT_COMPAT | ENT_HTML401 ]
3- string : Defines encoding used in conversion.  [Optional] Default value : [ string $encoding = ‘UTF-8’ ]

PHP chunk_split() Function Example

PHP Script:

<?php
echo chunk_split(“PHPTUTORS”,1,”_”);
echo “<br/ >”;
echo chunk_split(“Learn PHP on PHPTUTORS”,12,”…”);
?>

Output:

P_H_P_T_U_T_O_R_S_
Learn PHP on… PHPTUTORS…

Description:

chunk_split() function splits a string into a series of smaller chunks and append a specified string after each chunk as shown in above script example.

chunk_split function parameters:

1- String: String which is going to be spit into chunks
2- Chunk Length: Length of chunk [default is 76]
3- String: String which is appended at the end of each chunk