在其中一个元素上切片多维PHP数组

时间:2022-06-12 21:31:44

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

比如说你只是查询了一个数据库而你收到了这个2D数组。

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

我的问题是,有没有办法在不使用循环的情况下获取此数组$名称?使用回调函数计为使用循环。

Here is a more generic example of getting every field.

以下是获取每个字段的更一般示例。

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

10 个解决方案

#1


23  

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.

在上面提到的链接中有更多的例子。

#2


14  

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;
}

#3


7  

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.

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

#4


5  

Simply put, no.

简单地说,不。

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

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

#5


2  

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_map has a second use that allows you to do the inverse of what i want to do here. I also found a PHP class someone is writing to emulate prototypes manipulation of arrays.

我对此做了更多的研究,发现ruby和prototype都有一个函数,这个函数叫做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 internals@lists.php.net mailing list and see if they will add array_pluck.

我将要进行更多的挖掘,如果我找不到任何其他内容,我将处理一个补丁,提交到internals@lists.php.net邮件列表,看看他们是否会添加array_pluck。

#6


2  

For those of you that cannot upgrade to PHP5.5 right 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;
}

#7


1  

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和array_column()之前运行PHP版本,则可以使用普通PHP中的官方替换:

https://github.com/ramsey/array_column

https://github.com/ramsey/array_column

#8


0  

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所以我没有测试过这个。

#9


0  

You could do:

你可以这样做:

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

#10


0  

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;
    }
}

#1


23  

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.

在上面提到的链接中有更多的例子。

#2


14  

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;
}

#3


7  

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.

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

#4


5  

Simply put, no.

简单地说,不。

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

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

#5


2  

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_map has a second use that allows you to do the inverse of what i want to do here. I also found a PHP class someone is writing to emulate prototypes manipulation of arrays.

我对此做了更多的研究,发现ruby和prototype都有一个函数,这个函数叫做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 internals@lists.php.net mailing list and see if they will add array_pluck.

我将要进行更多的挖掘,如果我找不到任何其他内容,我将处理一个补丁,提交到internals@lists.php.net邮件列表,看看他们是否会添加array_pluck。

#6


2  

For those of you that cannot upgrade to PHP5.5 right 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;
}

#7


1  

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和array_column()之前运行PHP版本,则可以使用普通PHP中的官方替换:

https://github.com/ramsey/array_column

https://github.com/ramsey/array_column

#8


0  

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所以我没有测试过这个。

#9


0  

You could do:

你可以这样做:

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

#10


0  

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;
    }
}