PHP extract() Function Example

PHP Script:


<?php
$a = 'Carrot';
$sample_array = array("a" => "Apple","b" => "Banana", "c" => "Grapes");
extract($sample_array);
// Extraction Rule = EXTR_OVERWRITE [Default]
echo 'EXTR_OVERWRITE: $a = '.$a.' $b = '.$b.' $c = '.$c;
echo "< br/ >";

$a = 'Carrot';
extract($sample_array,EXTR_SKIP);
// Extraction Rule = EXTR_SKIP
echo 'EXTR_SKIP: $a = '.$a.' $b = '.$b.' $c = '.$c;
echo "< br/ >";

$a = 'Carrot';
extract($sample_array,EXTR_PREFIX_SAME,'copy1');
// Extraction Rule = EXTR_PREFIX_SAME
echo 'EXTR_PREFIX_SAME: $a = '.$a.' $b = '.$b.' $c = '.$c.' $copy1_a = '.$copy1_a;
echo "< br/ >";

$a = 'Carrot';
extract($sample_array,EXTR_PREFIX_ALL,'copy2');
// Extraction Rule = EXTR_PREFIX_ALL
echo 'EXTR_PREFIX_ALL: $copy2_a = '.$copy2_a.' $copy2_b = '.$copy2_b.' $copy2_c = '.$copy2_c;
echo "< br/ >";

$a = 'Carrot';
$sample_array2 = array("1" => "Apple","b" => "Banana", "c" => "Grapes");
extract($sample_array2,EXTR_PREFIX_INVALID,'copy3');
// Extraction Rule = EXTR_PREFIX_INVALID
echo 'EXTR_PREFIX_INVALID: $copy3_1 = '.$copy3_1.' $b = '.$b.' $c = '.$c;
echo "< br/ >";

$a = 'Carrot';
$sample_array3 = array("a" => "Cherry","b" => "Banana", "c" => "Grapes");
extract($sample_array3,EXTR_IF_EXISTS);
// Extraction Rule = EXTR_IF_EXISTS
echo 'EXTR_IF_EXISTS: $a = '.$a.' $b = '.$b.' $c = '.$c;
echo "< br/ >";

$a = 'Carrot';
extract($sample_array3,EXTR_PREFIX_IF_EXISTS,'copy4');
// Extraction Rule = EXTR_PREFIX_IF_EXISTS
echo 'EXTR_PREFIX_IF_EXISTS: $copy4_a = '.$copy4_a.' $b = '.$b.' $c = '.$c;
echo "< br/ >";

$a = 'Carrot';
extract($sample_array,EXTR_REFS);
// Extraction Rule = EXTR_REFS
echo 'EXTR_REFS: $a = '.$a.' $b = '.$b.' $c = '.$c;
?>

Output:


EXTR_OVERWRITE: $a = Apple $b = Banana $c = Grapes
EXTR_SKIP: $a = Carrot $b = Banana $c = Grapes
EXTR_PREFIX_SAME: $a = Carrot $b = Banana $c = Grapes $copy1_a = Apple
EXTR_PREFIX_ALL: $copy2_a = Apple $copy2_b = Banana $copy2_c = Grapes
EXTR_PREFIX_INVALID: $copy3_1 = Apple $b = Banana $c = Grapes
EXTR_IF_EXISTS: $a = Cherry $b = Banana $c = Grapes
EXTR_PREFIX_IF_EXISTS: $copy4_a = Cherry $b = Banana $c = Grapes
EXTR_REFS: $a = Apple $b = Banana $c = Grapes

Description:

PHP extract() function is used to import variables into the local symbol table from an array.

PHP extract() function parameters:

1- Array (Required)
2- Rules for extracting variables (Optional):
– This parameter tells how to treat with invalid and colliding names.
– Possible parametric values:
EXTR_OVERWRITE – Default. If names are collide existing variable is overwritten as shown in above example $a = 'Carrot' is overwrite by $a = 'Apple'.
EXTR_SKIP – If names are collide existing variable is not overwritten as shown in above example.
EXTR_PREFIX_SAME – If names are collide existing variable will be given a prefix as shown in above example.
EXTR_PREFIX_ALL – All variable names will be given a prefix as shown in above example.
EXTR_PREFIX_INVALID – Only invalid or numeric variable names will be given a prefix as shown in above example.
EXTR_IF_EXISTS – Only overwrite existing variables in the current symbol table, otherwise do nothing as shown in above example.
EXTR_PREFIX_IF_EXISTS – Only add prefix to variables if the same variable exists in the current symbol table as shown in above example.
EXTR_REFS – Extracts variables as references. The imported variables are still referencing the values of the array parameter as shown in above example.
3- Prefix (Optional):
– This parameter specifies the prefix. The prefix is automatically separated from the array key by an underscore character as shown in above example.
– It is required with following extract rules:
[EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID or EXTR_PREFIX_IF_EXISTS]

PHP array_search() Function Example

PHP Script:


<?php
$array = array(0 => 'book', 1 => 'cup', 'index'=>'text', 2 => 'spoon',3 => 'fork');
$key = array_search('text', $array);
echo "$key\n";
$key = array_search('cup', $array);
echo "$key\n";
?>

Output:


index
1

Description:

PHP array_search() function is used for searching an array for a given value and returns the corresponding array key if the given value is found.

PHP array_search() function parameters:

1- mixed: (required): The value to be searched.
2- Array (required): The array in which value is searched.
3- Bool(optional), Default False: This Boolean flag when set to true, then this function tires to check the type of the value begin searched and checks if both items references to the same  object.

PHP array_diff() Function Example

PHP Script:


<?php
$arr1=array("a"=>"Monkey","b"=>"Lion","c"=>"Tiger","d"=>"Cat");
$arr2=array("e"=>"Dog","f"=>"Donkey","g"=>"Horse","h"=>"Monkey");

$result=array_diff($arr1,$arr2);
print_r($result);

$arr3 = array("CE","EE","TE");
$arr4 = array("EE","BE","ME");
$arr5 = array("IM","TE","ME");

$result=array_diff($arr3,$arr4,$arr5);
print_r($result);
?>

Output:


Array
(
[b] => Lion
[c] => Tiger
[d] => Cat
)
Array
(
[0] => CE
)

Description:

PHP array_diff() function is used to compare the values of two or more arrays and returns the differences as an array which contains the elements from an Array1 that are not present in other arrays.

PHP array_diff() function parameters:

1- Array1 (required)
2- Array2 (required)
3- Array3 (optional)

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.