Monday, October 19, 2020

PHP array_column() Function



Hello folks! welcome to another section of our tutorial on PHP. In this tutorial guide, we are going to be studying about array_column() Function in PHP.

The PHP array_column() function returns the values from a single column of the input array and identified by the column_key.

You can optionally pass index_key to index the values in the returned array by the values from index_key column of the input array.

Syntax

Following below is the syntax to use this function -

array array_column( array $input , mixed $column_key [, mixed $index_key = NULL ] )


Parameter Details

Sr.NoParameter & Description
1

input (mandatory)

A multi-dimensional array or an array of objects from which to pull a column of values from.

2

column_key (mandatory)

The column of values to return. 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. This value can be NULL to return complete arrays or objects

3

index_key (optional)

The column to use as the index/keys for the returned array. This value may be the integer key of the column, or it may be the string key name.


Return Value

This function returns an array of values that represents a single column from the input array.

PHP Version

This function was first lunched as part of core PHP v 5.0.0. The ability for the input parameter to be an array of objects was first introduced in PHP 7.0.0

Example1

Try out the following example to get the column of the first name from a recordset -

<?php

 $records = array(
    array(
        'id' => 2135,
        'first_name' => 'Kennedy',
        'last_name' => 'Nkpara',
    ),
    array(
        'id' => 3245,
        'first_name' => 'Mary',
        'last_name' => 'Jane',
    ),
    array(
        'id' => 5342,
        'first_name' => 'Precious',
        'last_name' => 'Amah',
    ),
    array(
        'id' => 5623,
        'first_name' => 'Paul',
        'last_name' => 'Peters',
    )
  );

 $first_names = array_column($records, 'first_name');
 print_r($first_names);
?>

Output

When the above code is executed, it will produce the following result -

  Array
(
    [0] => Kennedy
    [1] => Mary
    [2] => Precious
    [3] => Paul
)

Example2

Now let's try one more example to get the column of first names from a recordset but this time we will index recordset using id -

<?php

 $records = array(
    array(
        'id' => 2135,
        'first_name' => 'Kennedy',
        'last_name' => 'Nkpara',
    ),
    array(
        'id' => 3245,
        'first_name' => 'Mary',
        'last_name' => 'Jane',
    ),
    array(
        'id' => 5342,
        'first_name' => 'Precious',
        'last_name' => 'Amah',
    ),
    array(
        'id' => 5623,
        'first_name' => 'Paul',
        'last_name' => 'Peters',
    )
 );
 $first_names = array_column($records, 'first_name', 'id');
 print_r($first_names);
?>

Output

When the above code is executed, it will produce the following result -

  Array
(
    [2135] => Kennedy
    [3245] => Mary
    [5342] => Precious
    [5623] => Paul
)


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the PHP array_combine() Function.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.
Share:

0 comments:

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.