ds34222 2015-05-12 20:27
浏览 121
已采纳

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

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.

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.

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.

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.

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

</div>
  • 写回答

1条回答 默认 最新

  • dptgpyl61857413 2015-05-12 20:29
    关注

    PHP >= 5.5.0:

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

    Older versions >= 5.3.0:

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

    For even older just use array_map with a concrete function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系