数组 函数
在线手册:中文  英文

array_column

(PHP 5 >= 5.5.0)

array_columnReturn the values from a single column in the input array

说明

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

array_column() returns the values from a single column of the input array, identified by the column_key. Optionally, you may provide an index_key to index the values in the returned array by the values from the index_key column in the input array.

参数

input

A multi-dimensional array (record set) from which to pull a column of values.

column_key

The column of values to return. This value may be the integer key of the column you wish to retrieve, or it may be the string key name for an associative array. It may also be NULL to return complete arrays (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.

返回值

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

范例

Example #1 Get column of first names from recordset

<?php
// Array representing a possible record set returned from a database
$records = array(
    array(
        
'id' => 2135,
        
'first_name' => 'John',
        
'last_name' => 'Doe',
    ),
    array(
        
'id' => 3245,
        
'first_name' => 'Sally',
        
'last_name' => 'Smith',
    ),
    array(
        
'id' => 5342,
        
'first_name' => 'Jane',
        
'last_name' => 'Jones',
    ),
    array(
        
'id' => 5623,
        
'first_name' => 'Peter',
        
'last_name' => 'Doe',
    )
);
 
$first_names array_column($records'first_name');
print_r($first_names);
?>

以上例程会输出:

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

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

<?php
// Using the $records array from Example #1
$last_names array_column($records'last_name''id');
print_r($last_names);
?>

以上例程会输出:

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


数组 函数
在线手册:中文  英文

用户评论:

jan venekamp (2013-06-26 16:00:25)

Implementation for PHP < 5.5.0
Following: https://wiki.php.net/rfc/array_column

<?php
// PHP < 5.5.0
if (!function_exists('array_column')) {
    function 
array_column($input$column_key$index_key null)
    {
        if (
$index_key !== null) {
            
// Collect the keys
            
$keys = array();
            
$i 0// Counter for numerical keys when key does not exist
            
            
foreach ($input as $row) {
                if (
array_key_exists($index_key$row)) {
                    
// Update counter for numerical keys
                    
if (is_numeric($row[$index_key]) || is_bool($row[$index_key])) {
                        
$i max($i, (int) $row[$index_key] + 1);
                    }
                    
                    
// Get the key from a single column of the array
                    
$keys[] = $row[$index_key];
                } else {
                    
// The key does not exist, use numerical indexing
                    
$keys[] = $i++;
                }
            }
        }
        
        if (
$column_key !== null) {
            
// Collect the values
            
$values = array();
            
$i 0// Counter for removing keys
            
            
foreach ($input as $row) {
                if (
array_key_exists($column_key$row)) {
                    
// Get the values from a single column of the input array
                    
$values[] = $row[$column_key];
                    
$i++;
                } elseif (isset(
$keys)) {
                    
// Values does not exist, also drop the key for it
                    
array_splice($keys$i1);
                }
            }
        } else {
            
// Get the full arrays
            
$values array_values($input);
        }
        
        if (
$index_key !== null) {
            return 
array_combine($keys$values);
        }
        
        return 
$values;
    }
}

?>

piotrek290 at gmail dot com (2013-06-25 21:14:19)

It's my array_column function for PHP < 5.5.0
function array_column($input, $column_key, $index_key=''){
$array_filtered = array();

if(is_array($input)){
foreach($input as $array){
if(is_array($array)){
foreach($array as $key => $value){
if($index_key){
if(array_key_exists($index_key, $array)){
if($key == $column_key){
$array_filtered[$array[$index_key]] = $value;
} else {
if($key == $column_key){
array_push($array_filtered, $value);
}
}
}
} else {
if($key == $column_key){
array_push($array_filtered, $value);
}
}
}
}
}
}

return $array_filtered;
}

vovan-ve at yandex dot ru (2013-05-17 14:49:46)

>>> for < PHP5.5 , following function can be used :
NEVER edit items of array argument by reference! It WILL damage items of original array, which are referenced outside with another variables like this:
$foo = array(10, 20, 30);
$bar = &$foo[1];
You should just to collect a new array instead of changing array by reference.
So, userland implementation can be such a following:
# PHP < 5.5
function array_column(array $input, $columnKey, $indexKey = null) {
$result = array();

if (null === $indexKey) {
if (null === $columnKey) {
// trigger_error('What are you doing? Use array_values() instead!', E_USER_NOTICE);
$result = array_values($input);
}
else {
foreach ($input as $row) {
$result[] = $row[$columnKey];
}
}
}
else {
if (null === $columnKey) {
foreach ($input as $row) {
$result[$row[$indexKey]] = $row;
}
}
else {
foreach ($input as $row) {
$result[$row[$indexKey]] = $row[$columnKey];
}
}
}

return $result;
}

peey (2013-05-17 09:27:40)

for < PHP5.5 , following function can be used :
function my_array_column($array,$column) {
foreach($array as &$value) {
$value = $value[$column];
}
return $array;
}

易百教程