AppDividend
Latest Code Tutorials

PHP Array Column Example | array_column() Function In PHP

0

PHP Array Column Example | array_column() Function In PHP is today’s topic. PHP array_column() function returns the values from the single column in an input array. It is an inbuilt function in PHP. The array_column() function is used to return the values from a single column in the input array.

PHP Array Column Example

The function returns an array which contains the values from the single column of an input array, identified by the 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.

See the following syntax.

array_column(array, column_key, index_key)

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.

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);

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 column of the scores 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 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 is used to return the values from the single column in an input array. The array_column does not support 1D arrays, in which case an empty array is returned. The $column_key is zero-based. If $column_key extends the valid index range an empty array is returned.

Finally, PHP Array Column Example | array_column() Function In PHP is over.

Recommended Posts

PHP array_udiff() Function Example

PHP count_chars() Function Example

PHP number_format() Function Example

PHP money_format() Function Example Tutorial

PHP unset() Function In PHP Tutorial

Leave A Reply

Your email address will not be published.

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