PHP array_column - 如何保存密钥?

时间:2014-11-29 16:49:29

标签: multidimensional-array php-5.5

    $items = array(
        1 => [
            "id" => 5
        ],

        3 => [
            "id" => 6
        ],
        4 => [
            "id" => 7
        ],
    );

    var_dump(array_column($items,"id"));

结果,

array (size=3)
  0 => int 5
  1 => int 6
  2 => int 7

但是如何保留$items的密钥以便我可以在下面得到这个?

array (size=3)
  1 => int 5
  3 => int 6
  4 => int 7

9 个答案:

答案 0 :(得分:18)

看看这是否有帮助

array_filter(array_combine(array_keys($items), array_column($items, 'id')));

答案 1 :(得分:6)

我认为这是保持密钥没有循环和迭代的最快方法

array_diff(array_combine(array_keys($items), array_column($items, 'id')), [null])

答案 2 :(得分:4)

寻找相同的解决方案并结合一些技巧,我创建了这个:

$userdb=Array
(
    "test1" => array
    (
        'uid' => '100',
        'name' => 'Sandra Shush',
        'url' => 'urlof100'
    ),
    "test2" => array
    (
        'uid' => '5465',
        'name' => 'Stefanie Mcmohn',
        'pic_square' => 'urlof100'
    ),
    "test3" => array
    (
        'uid' => '40489',
        'name' => 'Michael',
        'pic_square' => 'urlof40489'
    )
);

echo $key = array_search(
    40489,
    array_filter(
        array_combine(
            array_keys($userdb),
            array_column(
                $userdb, 'uid'
            )
        )
    )
);

结果是' test3'。

使用数组编号或命名数组。

答案 3 :(得分:2)

另一种方法是使用array_map

$result = array_map(function($item) {return $item['id'];}, $items);

答案 4 :(得分:2)

array_combine(array_keys($data), array_column($data, 'id'));

id => 要显示的列的名称

答案 5 :(得分:1)

foreach(key($parameters) as $key)
{
print($key);
}

如果需要,您还可以将结果存储在其他变量中。

要显示键和值,请尝试以下操作:

foreach ($parameters as $key => $value) {
echo $key . ' = ' . $value . '<br>';
}

答案 6 :(得分:1)

我编写了一个简单的函数array_column_keys,其参数与array_column相同。

/**
 * Return the values from a single column in the input array by keeping the key
 *
 * @param array $array     A multi-dimensional array (record set) from which to pull a column of values.
 * @param mixed $column    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).
 * @param mixed $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 array Returns an array of values representing a single column from the input array.
 */
function array_column_keys($array, $column, $index_key = null)
{
    $output = [];

    foreach ($array as $key => $item) {
        $output[@$item[$index_key] ?? $key] = @$item[$column];
    }

    return array_filter($output, function($item) {
        return null !== $item;
    });
}

我也需要第三个参数index_key。 如下例所示,这将在将第三个参数设置为null时回答该问题:

$result = array_column_keys($items, 'id');

...以及让我们定义键的值

$result = array_column_keys($items, 'id', 'any_key');

这将导致

array (size=3)
 string 'any_value1' => int 5
 string 'any_value2' => int 6
 string 'any_value3' => int 7

答案 7 :(得分:1)

最简单,最高效的方法可能是使用

array_combine(array_keys($data), array_column($data, 0));

答案 8 :(得分:-1)

仅适用于只有一列id

的示例
array_map('current', $items);
相关问题