Learn array_column php function in 1 minute

Function name: array_column php function

There are few things which you need to keep in mind before start to understand this tutorial. You should have basic understanding of php arrays & print_r function.

Syntax: array_column(array, column_key, index_key)
Support: (PHP 5>=5.5.0, PHP 7)
Return Type: array_column php function will always return array with single column from input array.

Conditions:

  • array(necessary)
  • column_key(necessary)
  • index_key (optional)

Description about array_column php function:

With the help of this function you can get or fetch all the data from single selected column from multi dimensional array. It’s very useful function.

Array: It’s a collection of information, from which we want to fetch information. It should be array of objects or multi-dimensional array.

Column key: It’s a key of that column which we want to fetch. For example we have multi-dimensional array of products having keys (id, title, description), and I want to extract the information of title. So, in second parameter I will put ‘title’ to fetch all the titles from array.

Index Key: It is use as the index/keys for the returned array. This value could be integer or string. For example, on above as we discussed, I was in need to fetch the record of title right? Now, without setting this third parameter I will only get an array with integer keys (that’s by default start’s from 0, 1, 2….) with selected values. If I say that index key is ‘id’ then all my keys will contain the values which are available in id. Don’t get confused. Check below examples.

Example 1:
In this example we have an multi-dimensional array of products, having id, title, description.
This example will show you that, how array_column php function works.

$products = array(
         array('id'=>201, 'title'=> 'iPhone 10', 'Description'=>'This is iphone 10'),
         array('id'=>202, 'title'=> 'C9 Pro', 'Description'=>'This is Samsung c9 pro'),
         array('id'=>203, 'title'=> 'C5 Pro', 'Description'=>'This is samsung c5 pro'),
         array('id'=>204, 'title'=> 'Laptop', 'Description'=>'This is Laptop i5'),
);

echo('<pre>');
  print_r(array_column($products, 'title'));
echo('</pre>');

Output: Without third paramter (index key)

Array
(
    [0] => iPhone 10
    [1] => C9 Pro
    [2] => C5 Pro
    [3] => Laptop
)

Example 2:
In this example, we are doing the same process as we did above only the difference is that, this time we are using third paramter that is index key.

$products = array(
         array('id'=>201, 'title'=> 'iPhone 10', 'Description'=>'This is iphone 10'),
         array('id'=>202, 'title'=> 'C9 Pro', 'Description'=>'This is Samsung c9 pro'),
         array('id'=>203, 'title'=> 'C5 Pro', 'Description'=>'This is samsung c5 pro'),
         array('id'=>204, 'title'=> 'Laptop', 'Description'=>'This is Laptop i5'),

);

echo('<pre>');
  print_r(array_column($products, 'title', 'id'));
echo('</pre>');

Output: with third parameter (index key)

Array
(
    [201] => iPhone 10
    [202] => C9 Pro
    [203] => C5 Pro
    [204] => Laptop
)

Learn array_column php function from Video Tutorial below:

Don’t forget to tweet it & subscribe for more tutorials:

Best Resource to learn PHP functions with in depth explanation. I just learned array_column within 1 minute from here Click To Tweet

Let me know if you’ll have issues in code in comments. I’ll wait for your feedback.

Don’t forget to share.

You will also like to learn array_chunk php function in 1 minute click here to learn this function.

Add Comment