PHP Array Functions With Examples


  • PHP array_flip() Function
  • PHP count() Function
  • PHP array_change_key_case ()
  • PHP array_search () Function
  • PHP array_sum () Function

    Share

    Array is the most important concept of the any programming language and array function made the array concept more power full so that we are adding this wonderful PHP array function tutorial and PHP array function tutorial PDF is available free of cost.

    1) PHP Array_flip() Function

    Syntax

        array_flip ( $array ); 
    

    Definition

    It returns an array with the keys of $array as values, and the values of $array as the new keys.

    Usage

    array_flip function is used to exchange the key as value and value as a key.

    Note:

    Keep in mind that array_flip() only flips the key/value mapping and does not reverse the positioning.

    Parameter Description
    Array array to flip (Required)

    Example Of Array_flip Function

    <?php
    $array = array
    (
     "a" => "apple",
     "b" => "banana",
     "o"=>"orange"
     );
    print_r( array_flip($array) );
    ?> 
    

    Output Of Given Php Script

    Array ( [apple] => a [banana] => b [orange] => o )



    2) PHP Count() Function

    Syntax

    count($array , mode) 
    

    Definition

    It returns number of element in an array.

    Usage

    Count function is used to find the number of element in an array.

    Note:

    If you want to count one dimensional array then option set 0 but default its zero so there is no requirement to set zero.
    In case of multidimensional array mode will set 1, it's compulsory.

    Parameter Description
    Array array to count (Required)
    Mode 0 for one dimensional array (optional)

    Example Of Array Count Function

    <?php
     $name = array ("php","java","html");
     $a=count($name);
     echo "Number of element in an array: ";
     echo $a;
     ?> 
    

    Output Of Given Php Script

    Number of element in an array: 3



    3) PHP Array Change Key Case Function

    Syntax

    array_change_key_case($array, option); 
    

    Definition

    It returns an array keys lowercase or uppercase as per condition.

    Usage

    array_change_key_case() function is used to change the case of arrays key.

    Note:

    Default option value is zero. It is optional.

    Parameter Description
    Array Option
    Any array variable (required) Option Default: 0 (lowercase) 1: (uppercase) (optional)

    Example Of Array Count Function

    <?php
     $student = array
    (
    "NamE" => "JOHN",
    "RolL NumbeR" => 38
    );
    print_r(array_change_key_case($student,1));
     ?> 
    

    Output Of Given Php Script

    Array ( [NAME] => JOHN [ROLL NUMBER] => 38 )



    Syntax

    array_search(search_value, $array , option); 
    

    Definition

    It returns the match value key of an array if element found otherwise it returns false.

    Usage

    array_search() function is used to find the element of an array and return its key. And after that you can access the element with help of key

    Note:

    Only the first instance of search value found returns its key. If the same Value is present later in the array, it is ignored.

    Parameter Description
    Search value Any data that you want to find (required)
    Array Any array variable (required)
    Option Option Default: false, match only vale not type and true for identical match

    Example Of Array Count Function

    <?php
    $num = array(20, 50, 10);
    $key = array_search(50, $num);
    Print_r($num);
     echo "
    Key of search no: ".$key; ?>

    Output Of Given Php Script

    Array ( [0] => 20 [1] => 50 [2] => 10 ) Key of search no: 1



    5) PHP Array_sum() Function

    Syntax

    array_sum($array); 
    

    Definition

    It returns sum of an array type an integer or float. .

    Usage

    array_sum() function is used to calculate the sum of an array.

    Note:

    Only the first instance of search value found returns its key. If the same Value is present later in the array, it is ignored.

    Parameter Description
    Array Any array variable (required)

    Example Of Array Count Function

    <?php
     $num = array(20, 50, 10);
     echo "sum of array = " . array_sum($num);
     ?> 
    

    Output Of Given Php Script

    Sum of array = 80