Tuesday, 31 January 2017

ARRAY FUNCTIONS IN PHP

1. count( ) – To get total number of elements in an array.

Example –
<?php
$arr=array(10,20,30);
echo count($arr);
?>

Output –
3


2. sort( ) – This function arranges array elements in ascending order based on values. It provides new keys to elements.

Example –
<?php
$arr=array(10,5,20,30);
sort($arr);
print_r($arr);
?>

Output –
Array
(
    [0] => 5
    [1] => 10
    [2] => 20
    [3] => 30
)


3. rsort( ) – Descending order with new keys.

Example –
<?php
$arr=array(10,5,20,30);
rsort($arr);
print_r($arr);
?>

Output –
Array
(
    [0] => 30
    [1] => 20
    [2] => 10
    [3] => 5
)


4. assort( ) – Ascending order with existing keys.

Example –
<?php
$arr=array(10,5,20,30);
asort($arr);
print_r($arr);
?>

Output –
Array
(
    [1] => 5
    [0] => 10
    [2] => 20
    [3] => 30
)


5. arsort( ) – Descending order with existing keys.

Example –
<?php
$arr=array(10,5,20,30);
arsort($arr);
print_r($arr);
?>

Output –
Array
(
    [3] => 30
    [2] => 20
    [0] => 10
    [1] => 5
)


6. ksort( ) – Ascending order based on keys.

Example –
<?php
$arr=array(100=>10,5,50=>20,30);
ksort($arr);
print_r($arr);
?>

Output –
Array
(
    [50] => 20
    [100] => 10
    [101] => 5
    [102] => 30
)


7. krsort( ) – Descending order based on keys.

Example –
<?php
$arr=array(100=>10,5,50=>20,30);
krsort($arr);
print_r($arr);
?>

Output –
Array
(
    [102] => 30
    [101] => 5
    [100] => 10
    [50] => 20
)


8. array_push( ) – Using this function we can add an element to the end of an array and return total number of elements.

Example –
<?php
$arr=array(10,20,30);
echo array_push($arr,40);
echo "\n";
print_r($arr);
?>

Output –
4
Array
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
)


9. array_pop( ) – This function removes last element of an array and returns value of that element.

Example –
<?php
$arr=array(10,20,30);
echo array_pop($arr);
echo "\n";
print_r($arr);
?>

Output –
30
Array
(
    [0] => 10
    [1] => 20
)


10. array_shift( ) – Removes first element of an array and returns value of that element.

Example –
<?php
$arr=array(10,20,30);
echo array_shift($arr);
echo "\n";
print_r($arr);
?>

Output –
10
Array
(
    [0] => 20
    [1] => 30
)


11. array_unshift( ) – This function adds an element in the beginning of an array and returns the total number of elements.

Example –
<?php
$arr=array(10,20,30);
echo array_unshift($arr,5);
echo "\n";
print_r($arr);
?>

Output –
4
Array
(
    [0] => 5
    [1] => 10
    [2] => 20
    [3] => 30
)


12. array_combine( ) – This function creates new array by combining the elements of two arrays. New array keys are values of first array. New array values are value of second array.

Example –
<?php
$arr1=array(10,20,30);
$arr2=array("X","Y","Z");
$narr=array_combine($arr1,$arr2);
print_r($narr);
?>

Output –
Array
(
    [10] => X
    [20] => Y
    [30] => Z
)

Note – Both Array must have equal number of array elements to combine, otherwise it will show error.


13. explode( ) – Using this function we can split a string as array elements based on input value.

Example –
<?php
$str="Welcome to PHP world";
$arr=explode(" ",$str);
print_r($arr);
?>

Output –
Array
(
    [0] => Welcome
    [1] => to
    [2] => PHP
    [3] => world
)


14. implode( ) – Using this function we can convert array elements as string based on input value.

Example –
<?php
$arr=array(10,20,30);
echo implode($arr,"/");
?>

Output –
10/20/30


15. extract( ) – This function converts associative array elements as variables. Variables name are element keys and values are element values.

Example –
<?php
$arr=array('X'=>10,20,30);         // key of 20 will be 0
$arr=array('X'=>10,'a'=>20,30);
extract($arr);
echo($X);
echo "\n";
echo($a);
?>

Output –
10
20


16. array_diff( ) – This function compares two arrays and returns difference from 1st array.

Example –
<?php
$arr1=array(10,20,30,40);
$arr2=array(30,40,50);
print_r(array_diff($arr1,$arr2));
?>

Output –
Array
(
    [0] => 10
    [1] => 20
)


17. array_diff_assoc( ) – This function compares both key and values and returns difference from 1st array.

Example –
<?php
$arr1=array(10,20,30,40);
$arr2=array(30,40,50);
print_r(array_diff_assoc($arr1,$arr2));
?>

Output –
Array
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
)

Example –
<?php
$arr1=array(10,100=>20,30,40);
$arr2=array(30,40,50);
print_r(array_diff_assoc($arr1,$arr2));
?>

Output –
Array
(
    [0] => 10
    [100] => 20
    [101] => 30
    [102] => 40
)


18. array_diff_key( ) – Compares keys of two arrays and returns difference from 1st array.

Example –
<?php
$arr1=array(10,100=>20,1,3);
$arr2=array(123,20,30,40);
print_r(array_diff_key($arr1,$arr2));
?>

Output –
Array
(
    [100] => 20
    [101] => 1
    [102] => 3
)


19. foreach – Using this statement, we can run a loop through the elements of an array.

Example –
<?php
$arr1=array(10,20,30,40);
foreach($arr1 as $var)
{
          echo "$var ";
}
echo "\n";
foreach($arr1 as $key => $var)
{
          echo "$key = $var \n";
}
?>

Output –
10 20 30 40
0 = 10
1 = 20
2 = 30
3 = 40 

No comments:

Post a Comment