Loading...
PHP

Array

  • Array is collection of heterogeneous element
  • PHP is loosely typed language so we can store heterogeneous elements is array.
  • The main difference between the normal variable and the array variable is the normal variable and the array variable is the normal variable can store the single value, array can store group of values.
  • Array is collection of elements each element is combination of elements key and value.
  • By default first element is starts from zero last element ends with element -1.

–> print _r:- This function is used to display all elements of array with keys and values.
<?php
$arr=array(10,20,”scoot”);
print_r($arr);
?>

Arrays are basically divided into two types they are
1. Numeric Array: Array elements with Numeric Array.
2. Associative Array: Array elements with string key is called as Associative Array.

<?php
$arr=array();
$arr[0]=10;
$arr[‘manger’]=”Scott”;
$arr[‘admin]=”Smith”;
print_r($arr);
?>

  1. count: This function is used to display total number of array elements.
    Example:
    <?php
    $arr=array(10,20,30);
    echo count($arr);
    ?>
  2. array_combine: This function is used to create new array by combining elements of two existing arrays. New array keys are value of first array new array value of second array.
    <?php
    $arr1=array(‘x’,’y’,’z’);
    $arr2=array(10,20,30);
    $narr=array_combine($arr1.$arr2);
    print_r($narr);
    ?>
  3. array_sum: This function is used to get the sum of all array elements.
    <?php
    $arr=array(10,20,30);
    echo array_sum($arr);
    ?>
  4. array_product: This function is used to get the product of elements.
    <?php
    $arr=array(10,20,30);
    echo array_product($arr);
    ?>
  5. Explode: This function is used to converts string as array elements based on input value.
    <?php
    $str=”Welcome to Webnoid”;
    #arr=explode(“0”,$str);
    print_r($arr);
    ?>
  6. Implode: This function is used to combine array elements as a string.
    <?php
    $arr=array(10,20,30);
    echo implode(“@”,$arr);
    ?>
  7. Extract: This function is used to divide the associative array element as variables from array.
    <?php
    $arr=array(‘a’=>10,’b’=>20);
    extract($arr);
    echo $a;
    echo $b;
    ?>
  8. list: This function is used assign array elements into the list of variables.
    <?php
    list($x,$y)=array(10,20);
    echo $x;
    echo $y;
    ?>
  9. in_array: This function is used to check whether the input value exists in the specified array or not. If the value exists then it returns true, else it returns false
    <?php
    $arr=array(10,20,30);
    echo in_array(20,$arr);
    ?>
  10. array_search: This function is used to search whether the input value exists in specific array or not and returns key of that element.
    <?php
    $arr=array(10,20,30);
    echo array_search(20, $arr);
    ?>
  11. array_rand: This function is used to get the keys of array randomly.
    <?php
    $arr=array(‘b’=>10,’a’=>20);
    print_r(array_rand($arr,2));
    ?>
  12. array_slice: This function is used to get the same part of an array.
    <?php
    $arr=array(10,20,30,40);
    print_r(array_slice($arr,0,3));
    ?>
  13. array_count_values: This function returns an array with number of occurrence for each value.
    <? php
    $arr=array(10,20,30,40);
    print_r(array_count_values($arr));
    ?>
  14. array_key_exists: This function is to check whether the input key exists or not in the array.
  15. array_keys: This function returns all keys of an array as a new array.
    <?php
    $arr=array(x=>10,’a’=>20,’b’=>40);
    print_r(array_keys($arr));
    ?>
  16. array_merge: This is used to merge the elements of two arrays.
    <?php
    $arr=array(10,20,30);
    $arr1=array(40,50);
    $na=array_merge($arr,$arr1);
    print_r($na);
    ?>
  17. array_reverse: This function display array elements in the reverse.
    <?php
    $arr=array(10,5,20);
    print_r(array_reverse($arr);
    ?>
  18. array_values: This functions returns all the values of array as a new array.
    <?php
    $arr=array(10,’a’=>5,20);
    print_r(array_values($arr));
    ?>
  19. array_shuffle: This function shuffles the elements of an array.
    <?php
    arr=array(10,’a’=>5,20);
    shuffle($arr);
    print_r($arr);
    ?>
  20. array_diff: This  function compare the two arrays and get the difference from the first array.
    <?php
    $arr1=array(10,20,30,40);
    $arr2=array(10,30);
    print_r(array_diff($arr1,$arr2);
    ?>
  21. array_diff__keys: This function compare the keys of two arrays return the difference from first array.
    <php
    $arr=array(10,20,30,40);
    $arr=array(10,30);
    print_r(array_diff_key($arr1,$arr2));
    ?>
  22. array_diff_assoc: This function compares both keys and values of two arrays and returns the difference from first array.
    <?php
    $arr1=array(10,20,30,40);
    $arr2=array(10,30);
    print_r(array_diff_assoc($arr1,$arr2));
    ?>
  23. sort: This function arranges array elements in ascending order based on the values and creates new array with those elements.
    <?php
    $arr=array(10,30,20,40);
    sort($arr);
    print_r($arr);
    ?>
  24. rsort: This function arranges the array elements in descending order with new keys.
    <?php
    $arr=array(10,’x’=>30,20);
    rsort($arr);
    print_r($arr);
    ?>
  25. asort: This function arranges the array elements with existing keys.
    <?php
    $arr(10,’x’=>30,20);
     asort($arr);
     print_r($arr);
    ?>
  26. arsort: This function arranges the array elements in descending order with existing keys
    <?php
    $arr(10,’x’=>30,20);
     arsort($arr);
     print_r($arr);
    ?>
  27. ksort: This function arranges the array elements in ascending order based on keys.
    <?php
    $arr=array(10,100=>20,50=>30);
    ksort($arr);
    print_r($arr);
    ?>
  28. krsort: This functions displays as it is according to keys.
  29. array_push: This function is used to add an element at the end of array returns total number of elements.
    <?php
    $arr=array(10,20,30);
    echo array_push($arr,40);
    print_r($arr;
    ?>
  30. array_pop: This function removes last element of an array and returns the last element of array.
    <?php
    $arr(10,20,30);
     echo array_pop($arr);
     print_r($arr);
    ?>
  31. array_shift: This removes the first element of an array returns the value of that element.
    <?php
    $arr=array(10,20,30);
    echo array_shift($arr);
    print_r($arr);
    ?>
  32. array_unshift: This function adds an element at the beginning of an array and return total number of elements 
    <?php
    $arr=array(10,20,30);
    echo array_unshift($arr,5);
    print_r($arr);
    ?>
  33. foreach:  This loop is used to read each of every element of array along with keys and values.
  34. multi dimensional array: This concept is used to create an array within another array.
    <?php
    $arr=array(100=>array(‘x’.’y’),20,30);
    echo $arr[10][0];
    print_r($arr);
    ?>

Leave a Reply

Your email address will not be published. Required fields are marked *