Use array_column() to Get Values from Multidimensional Arrays

Previously we saw how we can create Multidimensional Arrays. Now we’ll see how we can get specific key, values from multidimensional arrays using the array_column() function.

get values from multidimensional arrays

The array_column() function fetches the values from a single column in the input array. We have to specify the array we are going to use and the column_key (An integer key or a string key name of the column of values to return). Without these two things, array_column() function will not work.

Let’s check an example :

Here array_column() function will return the Values with ‘Address’ key from the $users array, which is a multidimensional array.

Output will be like this :

Array ( [0] => Bangladesh [1] => Earth [2] => Bangladesh )

We didn’t provide any index key here. So, by default it used the numeric index key with the values.

Let’s see how we can provide index keys. What we will do is, we’ll use another column as the index key.

We used the ‘id’ column as the index keys. Output is like this :

Array ( [10] => Student [20] => Freelancer [30] => Blogger )

Similarly you can use any other array key values as the index key. For example if I used ‘Occupation’ instead of ‘id’ as the key here, output would be like this :

Array ( [Student] => 10 [Freelancer] => 20 [Blogger] => 30 )

Hope you find this helpful. If you have any queries, let me know through comments.

Click Here to Leave a Comment Below

javfinder.com

This value may be an integer key of the column you wish to retrieve, or it may be a string key name for an associative array or property name.

Reply
    turletee.com

    Sometimes, we require to remove duplicates value from multidimensional array in our PHP framework php like laravel, codeigniter, zend etc.

    Reply
Leave a Reply: