[英]Slicing a multi-dimensional PHP array across one of its elements
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?我的问题是有没有办法在不使用循环的情况下获取这个数组 $names ? 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;
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. 在上面提到的链接中有更多的例子。
I voted @Devon's response up because there really isn't a way to do what you're asking with a built-in function. 我投了@Devon的回复,因为实际上没有办法用内置函数做你想要的。 The best you can do is write your own: 你能做的最好就是写自己的:
function array_column($array, $column)
{
$ret = array();
foreach ($array as $row) $ret[] = $row[$column];
return $ret;
}
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. 这将返回按“名称”维度切片的数组。
Simply put, no. 简单地说,不。
You will need to use a loop or a callback function like array_walk . 您将需要使用循环或回调函数,如array_walk 。
I did more research on this and found that ruby and prototype both have a function that does this called array_pluck , 2 . 我对此做了更多的研究,发现ruby和prototype都有一个叫做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. 有趣的是, array_map
有第二次使用,允许你做我想做的反向。 I also found a PHP class someone is writing to emulate prototypes manipulation of arrays. 我还发现了一个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。
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;
}
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
You could do: 你可以这样做:
$tmp = array_flip($names);
$names = array_keys($tmp);
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所以我没有测试过这个。
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;
}
}
other alternative其他选择
function transpose(array $array): array
{
$out = array();
foreach ($array as $rowkey => $row) {
foreach ($row as $colkey => $col) {
$out[$colkey][$rowkey] = $col;
}
}
return $out;
}
function filter_columns(array $arr, string ...$columns): array
{
return array_intersect_key($arr, array_flip($columns));
}
test测试
$results = array(
array('id' => 1, 'name' => 'red' , 'spin' => 1),
array('id' => 2, 'name' => 'green', 'spin' => -1),
array('id' => 3, 'name' => 'blue' , 'spin' => .5)
);
var_dump(filter_columns(transpose($results),'name'));
var_dump(filter_columns(transpose($results),'id','name'));
var_dump(filter_columns(transpose($results),'id','spin'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.