Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to fetch keys and values of an array

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 219
    Comment on it

    This article provides the details about some PHP functions which are used to fetch keys and values of an array. The article also defines a function which can also used to fetch all values from a single column of a multidimensional array. The details and example of each function are given below :

     

    1. array_keys

    array_keys() function is used to get all or subsets of keys of an array. This function returns an array of keys from the input array. If an optional search value is specified, then the only keys for that value are returned, otherwise all keys are returned as an array with numerical indexes.

     

    Syntax : array_keys(array, value, strict_mode)

    array : Specifies an input array.(Required)

    value : Specify an array value, if value is passed as parameter only keys containing this value are returned.(Optional)

    strict_mode : Specify the comparison mode.(Optional)

     

    If value for strict_mode is set to TRUE, then the comparison for value is performed using the equality operator(===). The default value is FALSE and comparison takes place using == operator.

     

    Result : The function returns an array of keys.

     

    Changelog : From PHP version 5.0 strict_mode parameter was added.

     

    Example 1 :

    <?php
    $keys = array();
    $array=array("d" => "orange", "b" => "apple", "e" => "mango", "a" => "pear", "c" => "grapes");
    $keys = array_keys($array);
    ?>

    Output :

    Array
    (
        [0] => d
        [1] => b
        [2] => e
        [3] => a
        [4] => c
    )

     

    Example 2 : Using value parameter

    <?php
    $array1 = array("a"=>"5","b"=>"2","c"=>"5","d"=>"1","e"=>"4");
    $keys = array_keys($array1, 5);
    print_r($keys);
    ?>

    Output :

    Array
    (
        [0] => a
        [1] => c
    )

     

    Example 3 : Using strict_mode as TRUE

    <?php
    $array1 = array("a"=>"5","b"=>"2","c"=>"5","d"=>"1","e"=>"4");
    $keys = array_keys($array1, "5", true);
    print_r($keys);
    ?>

    Output :

    Array
    (
        [0] => a
        [1] => c
    )

     

     

    2. array_values

    array_values() function is used to get all the values of an array. This function returns an array of all values from the input array. The returned array has numeric keys start from 0.

     

    Syntax : array_values(array)

    array :  Specify an input array.(Required)

     

    Result : This function returns an array of values from the input array.

     

    Example :

    <?php
    $array=array("d" => "orange", "b" => "apple", "e" => "orange", "a" => "pear", "c" => "orange");
    $values = array_values($array);
    echo "<pre>";
    print_r($values);
    echo "</pre>";
    ?>

    Output :

    Array
    (
        [0] => orange
        [1] => apple
        [2] => orange
        [3] => pear
        [4] => orange
    )

     

     

    3. array_column

    array_column() function is mostly used with associative array to get all values from a single column of the input array. It returns an array of values of the specified column.

     

    Syntax : array_column(array, column_key, index_key)

    array : Specifies a multidimensional array.(Required)

    column_key : Specify column index or name whose value has to return. This field can be an integer key (index) or string (column name). It can also be NULL to return values of all columns.(Required)

    index_key : Defines an optional field to index the resultant array.(Optional)

     

    Description : This function returns an array with values of a single column specified by column_key of a multidimensional array. An optional index_key can also be passed as parameter to index the resultant array. index_key can be an integer key or may be the value of another column name of the array.

     

    Result : This function returns an array of values from the specified column of the array.

     

    Example :

    <?php
    $employee = array(
    	array("id" => "2", "emp_name" => "Rohit", "age" => "28"),
    	array("id" => "7", "emp_name" => "Amit", "age" => "31"),
    	array("id" => "8", "emp_name" => "Rahul", "age" => "26"),
    	array("id" => "10", "emp_name" => "Deepak", "age" => "25")
    );
    echo "<pre>";
    print_r(array_column($employee, 'emp_name'));
    echo "</pre>";
    ?>

    Output :

    Array
    (
        [0] => Rohit
        [1] => Amit
        [2] => Rahul
        [3] => Deepak
    )

     

    Example 2 : With index_key

    <?php
    $employee = array(
    	array("id" => "2", "emp_name" => "Rohit", "age" => "28"),
    	array("id" => "7", "emp_name" => "Amit", "age" => "31"),
    	array("id" => "8", "emp_name" => "Rahul", "age" => "26"),
    	array("id" => "10", "emp_name" => "Deepak", "age" => "25")
    );
    echo "<pre>";
    print_r(array_column($employee, 'emp_name','id'));
    echo "</pre>";
    ?>

    Output :

    Array
    (
        [2] => Rohit
        [7] => Amit
        [8] => Rahul
        [10] => Deepak
    )

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: