PHP:从关联数组中的键返回值数组[重复]

时间:2022-04-04 17:12:19

This question already has an answer here:

这个问题在这里已有答案:

Let's say that I have an array like this:

假设我有一个这样的数组:

$people = array(
    array(
        'name' => 'Bob',
        'job'  => 'Carpenter'
    ),

    array(
        'name' => 'George',
        'job'  => 'Programmer'
    ),

    array(
        'name' => 'Clint',
        'job'  => 'Actor'
    )
);

And I want to know the names of all of these people.

我想知道所有这些人的名字。

I know that I could do this:

我知道我可以这样做:

$names = array();

foreach($people as $person)
    $names[] = $person['name'];

But let's say that I'm lazy, so I create a function for it instead:

但是,让我说我很懒,所以我为它创建了一个函数:

/**
 * Returns an Array of Values paired with the specified $key
 * in the $array of Associative Arras.
 *
 * @param array  $array The Array of Associative Arrays.
 * @param string $key   The Key within each Associative Array to retrieve the Value
 *
 * @return array The Array of Keyed Values within the Array of Arrays.
 */
function array_keyed_values($array, $key)
{
    $values = array();

    foreach($array as $associative)
        if(array_key_exists($key, $associative))
            $values[] = $associative[$key];

    return $values;
}

Cool, I've officially solved my problem. I just need to do:

很酷,我已经正式解决了我的问题。我只需要这样做:

$names = array_keyed_values($people, 'name');

To get:

(Bob, George, Clint)

And I'm done.

而且我已经完成了。

However, I doubt that I'm the first person to need this kind of functionality. Given that PHP is littered with a plethora of quirky functions that do weird things, I'm wondering if something like this already exists.

但是,我怀疑我是第一个需要这种功能的人。鉴于PHP充斥着过多奇怪的功能,这些功能很奇怪,我想知道这样的事情是否已经存在。

Yes, I know, I'm only saving myself 10 to 20 lines of code by not writing the function I need, but in a framework that uses tons of files, having to constantly include a library to do something this simple tends to get a little tedious.

是的,我知道,我只是通过不编写我需要的功能来保存自己10到20行代码,但是在一个使用大量文件的框架中,不得不经常包含一个库来做一些这样简单的事情往往得到一个有点乏味。

I've tried searching around for this, but I might not be using the right keywords. Something like this involves arrays, keys, and values, so searching for that type of stuff tends to constantly point me to array_keys() or array_values(), neither of which is what I want.

我已经尝试过搜索这个,但我可能没有使用正确的关键字。像这样的东西涉及数组,键和值,所以搜索那种类型的东西往往会不断指向array_keys()或array_values(),这两者都不是我想要的。

NOTE:

For the particular application I'm using, the ordering of the values when returned does not matter.

对于我正在使用的特定应用程序,返回时值的排序无关紧要。

Also, the version of PHP I'm using is 5.3. If a later version of PHP adds this functionality, please state that in your answer.

另外,我使用的PHP版本是5.3。如果更高版本的PHP添加了此功能,请在答案中说明。

EDIT:

The following solution worked in this scenario:

以下解决方案适用于此方案:

$result = array_map(function($v) { return $v['name']; }, $people);

However, I also used this to solve a similar problem where I had an array of objects (where the nested arrays where the objects, and the keys were the variables). By slightly modifying the code above, I solved that problem too:

但是,我也使用它来解决类似的问题,其中我有一个对象数组(其中嵌套数组的对象和键是变量)。稍微修改上面的代码,我也解决了这个问题:

$result = array_map(function($v) { return $v->name; }, $people);

While array_column() works for my scenario (if I were using PHP 5.5+ instead of PHP 5.3), it wouldn't work for the modified solution above (Hence the edit).

虽然array_column()适用于我的场景(如果我使用PHP 5.5+而不是PHP 5.3),它对上面修改的解决方案(因此编辑)不起作用。

1 个解决方案

#1


PHP >= 5.5.0:

PHP> = 5.5.0:

$result = array_column($people, 'name');

Older versions >= 5.3.0:

旧版本> = 5.3.0:

$result = array_map(function($v) { return $v['name']; }, $people);

For even older just use array_map with a concrete function.

对于更老的人来说,只需使用具有特定功能的array_map即可。

#1


PHP >= 5.5.0:

PHP> = 5.5.0:

$result = array_column($people, 'name');

Older versions >= 5.3.0:

旧版本> = 5.3.0:

$result = array_map(function($v) { return $v['name']; }, $people);

For even older just use array_map with a concrete function.

对于更老的人来说,只需使用具有特定功能的array_map即可。