Hướng dẫn array_column multidimensional php

[PHP 5 >= 5.5.0, PHP 7, PHP 8]

array_columnReturn the values from a single column in the input array

Description

array_column[array $array, int|string|null $column_key, int|string|null $index_key = null]: array

Parameters

array

A multi-dimensional array or an array of objects from which to pull a column of values from. If an array of objects is provided, then public properties can be directly pulled. In order for protected or private properties to be pulled, the class must implement both the __get[] and __isset[] magic methods.

column_key

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. It may also be null to return complete arrays or objects [this is useful together with index_key to reindex the array].

index_key

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. The value is cast as usual for array keys [however, prior to PHP 8.0.0, objects supporting conversion to string were also allowed].

Return Values

Returns an array of values representing a single column from the input array.

Changelog

VersionDescription
8.0.0 Objects in columns indicated by index_key parameter will no longer be cast to string and will now throw a TypeError instead.

Examples

Example #1 Get the column of first names from a recordset

The above example will output:

Array
[
    [0] => John
    [1] => Sally
    [2] => Jane
    [3] => Peter
]

Example #2 Get the column of last names from a recordset, indexed by the "id" column

The above example will output:

Array
[
    [2135] => Doe
    [3245] => Smith
    [5342] => Jones
    [5623] => Doe
]

Example #3 Get the column of usernames from the public "username" property of an object

The above example will output:

Array
[
    [0] => user 1
    [1] => user 2
    [2] => user 3
]

Example #4 Get the column of names from the private "name" property of an object using the magic __get[] method.

The above example will output:

Array
[
    [0] => Fred
    [1] => Jane
    [2] => John
]

If __isset[] is not provided, then an empty array will be returned.

mohanrajnr at gmail dot com

7 years ago

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

    }

}

WARrior

8 years ago

You can also use array_map fucntion if you haven't array_column[].

example:

$a = array[
    array[
        'id' => 2135,
        'first_name' => 'John',
        'last_name' => 'Doe',
    ],
    array[
        'id' => 3245,
        'first_name' => 'Sally',
        'last_name' => 'Smith',
    ]
];

array_column[$a, 'last_name'];

becomes

array_map[function[$element]{return $element['last_name'];}, $a];

balbuf

4 years ago

This function does not preserve the original keys of the array [when not providing an index_key].

You can work around that like so:

ff2 AT hotmail DOT co DOT uk

4 years ago

Because the function was not available in my version of PHP, I wrote my own version and extended it a little based on my needs.

When you give an $indexkey value of -1 it preserves the associated array key values.

EXAMPLE:

$sample = array[
    'test1' => array[
        'val1' = 10,
        'val2' = 100
    ],
    'test2' => array[
        'val1' = 20,
        'val2' = 200
    ],
    'test3' => array[
        'val1' = 30,
        'val2' = 300
    ]
];

print_r[array_column_ext[$sample,'val1']];

OUTPUT:

Array
[
    [0] => 10
    [1] => 20
    [2] => 30
]

print_r[array_column_ext[$sample,'val1',-1]];

OUTPUT:

Array
[
    ['test1'] => 10
    ['test2'] => 20
    ['test3'] => 30
]

print_r[array_column_ext[$sample,'val1','val2']];

OUTPUT:

Array
[
    [100] => 10
    [200] => 20
    [300] => 30
]

yangmeishu at live dot com

2 years ago

Please note that if you use array_column to reset the index, when the index value is null, there will be different results in different PHP versions, examples

Chủ Đề