Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • get a array of a column from a multidimensional array

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 84
    Comment on it

    Hello friends,

    Here I am explaining that if you want to get all the values from a specific column from a multidimensional array then you can use a php predefined function which is "array_column()".
    array_column returns the values from a single column in the input array.

    Here is a example which will explain you clearly-

    <?php
    
    $userrecords = array(
        array(
            'id' => 1234,
            'first_name' => 'John',
            'last_name' => 'Cena',
        ),
        array(
            'id' => 3245,
            'first_name' => 'Julia',
            'last_name' => 'Robert',
        ),
        array(
            'id' => 5342,
            'first_name' => 'James',
            'last_name' => 'Bond',
        ),
        array(
            'id' => 5623,
            'first_name' => 'Peter',
            'last_name' => 'Cena',
        )
    );
    
    $first_names = array_column($userrecords, 'first_name');
    print_r($first_names);
    ?>
    

    Output:

    Array
    (
        [0] => John
        [1] => Julia
        [2] => James
        [3] => Peter
    )
    

 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: