PHP array_column: The Complete Guide

0
112
PHP Array Column Example | array_column() Function In PHP

The array_column() function returns the values from a single column in the input array.

PHP array column

PHP array_column() is a built-in function that returns the values from the single column in an input array, identified by column_number. Optionally, the index_key may also be provided to index the values in the returned array by the values from an index_key column of an input array.

Syntax

See the following syntax.

array_column(array, column_key, index_key)

Parameters

The array is a required parameter, and it specifies a multi-dimensional array (record-set) to use.

As of PHP 7.0, the array can also be an array of objects.

The column_key parameter is required. The integer key or the string key name of the column of values to return. The column_key parameter can also be NULL to return the complete arrays.

The index_key parameter is required. The integer key or the string key name of the column of values to return. The parameter can also be NULL to return complete arrays.

Example

See the following code.

<?php

  $arr = array(['cname' => 'Eleven', 'rname' => 'Millie'],
              ['cname' => 'Mike', 'rname' => 'Finn'],
              ['cname' => 'Dustin', 'rname' => 'Gaten']);

  $op = array_column($arr, 'rname');
  print_r($op);

Output

See the output.

➜  pro php app.php
Array
(
    [0] => Millie
    [1] => Finn
    [2] => Gaten
)
➜  pro

The array_column() with index_key

See the following code.

<?php

$data = array(
    array(
        'id' => 11,
        'first_name' => 'Millie',
        'last_name' => 'Brown',
    ),
    array(
        'id' => 21,
        'first_name' => 'Sam',
        'last_name' => 'Smith',
    ),
    array(
        'id' => 19,
        'first_name' => 'Jessica',
        'last_name' => 'Jones',
    ),
    array(
        'id' => 22,
        'first_name' => 'Jon',
        'last_name' => 'Snow',
    )
);
 
$op = array_column($data, 'first_name', 'id');
print_r($op);

See the output.

➜  pro php app.php
Array
(
    [11] => Millie
    [21] => Sam
    [19] => Jessica
    [22] => Jon
)
➜  pro

Use of array_column() with OOP

Get the scores column from the public “score” property of an object.

<?php

// App.php

class App
{
    public $score;

    public function __construct(string $score)
    {
        $this->score = $score;
    }
}

$scores = [
    new App(8),
    new App(9),
    new App(10),
];

print_r(array_column($scores, 'score'));

See the output.

➜  pro php app.php
Array
(
    [0] => 8
    [1] => 9
    [2] => 10
)
➜  pro

If the function is not available

If array_column does not exist, the below solution will work.

if(!function_exists("array_column"))
{
    function array_column($array,$column_name)
    {
        return array_map(function($element) use($column_name){return $element[$column_name];}, $array);
    }
}

That means you can also use the array_map() function if you haven’t array_column().

Note

This function does not preserve the original keys of the array (when not providing an index_key).

Conclusion

PHP array_column() method returns the values from the single column in an input array. The array_column does not support 1D arrays, in which an empty array is returned.

The $column_key is zero-based. If $column_key extends the valid index range an empty array is returned.

That’s it for PHP array_column() function.

Recommended Posts

PHP array_udiff()

PHP count_chars()

PHP number_format()

PHP money_format()

PHP unset()

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.