将多维 PHP 数组切分到其元素之一

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/163336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:44:21  来源:igfitidea点击:

Slicing a multi-dimensional PHP array across one of its elements

phparraysassociative-array

提问by gradbot

Say for example you just queried a database and you recieved this 2D array.

例如,假设您刚刚查询了一个数据库并收到了这个二维数组。

$results = array(
    array('id' => 1, 'name' => 'red'  , 'spin' =>  1),
    array('id' => 2, 'name' => 'green', 'spin' => -1),
    array('id' => 3, 'name' => 'blue' , 'spin' => .5)
);

I often find myself writing loops like this.

我经常发现自己在写这样的循环。

foreach($results as $result)
    $names[] = $result['name'];

My questions is does there exist a way to get this array $names without using a loop? Using callback functions count as using a loop.

我的问题是有没有办法在不使用循环的情况下获取这个数组 $names ?使用回调函数算作使用循环。

Here is a more generic example of getting every field.

这是获取每个字段的更通用示例。

foreach($results as $result)
    foreach($result as $key => $value)
        $fields[$key][] = $value;

回答by Salvador Dali

As of June 20th in PHP-5.5 there is a new function array_column

截至 6 月 20 日,PHP-5.5 中有一个新函数array_column

For example:

例如:

$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'
    )
);


$firstNames = array_column($records, 'first_name');
print_r($firstNames);

Will return

将返回

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

There are even more examples in the above mentioned link.

上面提到的链接中还有更多示例。

回答by inxilpro

I voted @Devon's response up because there really isn't a way to do what you're asking with a built-in function. The best you can do is write your own:

我对@Devon 的回答投了赞成票,因为真的没有办法用内置函数来完成你的要求。你能做的最好的事情就是自己写:

function array_column($array, $column)
{
    $ret = array();
    foreach ($array as $row) $ret[] = $row[$column];
    return $ret;
}

回答by Alexey Petushkov

Starting PHP 5.3, you can use this pretty call with lambda function:

从 PHP 5.3 开始,您可以在 lambda 函数中使用这个漂亮的调用:

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

This will return array sliced by 'name' dimension.

这将返回按“名称”维度切片的数组。

回答by Devon

Simply put, no.

简单地说,没有。

You will need to use a loop or a callback function like array_walk.

您将需要使用循环或回调函数,如array_walk

回答by gradbot

I did more research on this and found that ruby and prototype both have a function that does this called array_pluck,2. It's interesting that array_maphas a second use that allows you to do the inverse of what i want to do here. I also found a PHP classsomeone is writing to emulate prototypes manipulation of arrays.

我对此进行了更多研究,发现 ruby​​ 和原型都有一个名为array_pluck, 2的函数。有趣的是,array_map它还有第二个用途,可以让您做与我在这里想做的相反的事情。我还发现有人正在编写一个 PHP来模拟数组的原型操作。

I'm going to do some more digging around and if I don't find anything else I'll work on a patch to submit to the [email protected] mailing list and see if they will add array_pluck.

我将做更多的挖掘工作,如果我找不到其他任何东西,我将制作补丁以提交到 [email protected] 邮件列表,看看他们是否会添加 array_pluck。

回答by MirroredFate

For those of you that cannot upgrade to PHP5.5right now and need this function, here is an implementation of array_column.

对于那些PHP5.5现在无法升级并需要此功能的人,这里是array_column.

function array_column($array, $column){
    $a2 = array();
    array_map(function ($a1) use ($column, &$a2){
        array_push($a2, $a1[$column]);
    }, $array);
    return $a2;
}

回答by JohnK

If you are running a version of PHP before 5.5 and array_column(), you can use the official replacement in plain PHP:

如果您运行的是 5.5 和 之前的 PHP 版本array_column(),则可以使用普通 PHP 中的官方替换:

https://github.com/ramsey/array_column

https://github.com/ramsey/array_column

回答by Keshav Kalra

This is fast function alternative of array_column()

这是 array_column() 的快速函数替代

if(!function_exists('array_column')) {
    function array_column($element_name) {
        $ele =   array_map(function($element) {
            return $element[$element_name];
        }, $a);
        return  $ele;
    }
}

回答by yokototo

You could do:

你可以这样做:

$tmp = array_flip($names);
$names = array_keys($tmp);

回答by Bill

I think this will do what you want

我认为这会做你想做的

array_uintersect_uassoc

array_uintersect_uassoc

You would have to do something like this

你将不得不做这样的事情

$results = array(
    array('id' => 1, 'name' => 'red'  , 'spin' =>  1),
    array('id' => 2, 'name' => 'green', 'spin' => -1),
    array('id' => 3, 'name' => 'blue' , 'spin' => .5)
);
$name = array_uintersect_uassoc( $results, array('name' => 'value')  , 0, "cmpKey");
print_r($name);

//////////////////////////////////////////////////
// FUNCTIONS
//////////////////////////////////////////////////
function cmpKey($key1, $key2) {
  if ($key1 == $key2) {
    return 0;
  } else {
    return -1;
  }
}

However, I don't have access to PHP5 so I haven't tested this.

但是,我无法访问 PHP5,所以我还没有对此进行测试。