php 如何在不循环的情况下获取多维数组中特定“键”的数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7994497/
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
How to get an array of specific "key" in multidimensional array without looping
提问by ifaour
Let's assume I have the following multidimensional array (retrieved from MySQL or a service):
假设我有以下多维数组(从 MySQL 或服务中检索):
array(
array(
[id] => xxx,
[name] => blah
),
array(
[id] => yyy,
[name] => blahblah
),
array(
[id] => zzz,
[name] => blahblahblah
),
)
Can we get an array of id
s in one "built-in" php function call? or one line of code?
I am aware of the traditional looping and getting the value but I don't need this:
我们可以id
在一个“内置”php 函数调用中获得一组s吗?还是一行代码?
我知道传统的循环并获得价值,但我不需要这个:
foreach($users as $user) {
$ids[] = $user['id'];
}
print_r($ids);
Maybe some array_map()
and call_user_func_array()
can do the magic.
也许有些array_map()
和call_user_func_array()
可以做的魔力。
回答by phihag
Since php 5.5, you can use array_column
:
从 php 5.5 开始,您可以使用array_column
:
$ids = array_column($users, 'id');
This is the preferred option on any modern project. However, if you must support php > 5.5, the following alternatives exist:
这是任何现代项目的首选选项。但是,如果您必须支持 php > 5.5,则存在以下替代方案:
Since php 5.3, you can use array_map
with an anonymous function, like this:
从 php 5.3 开始,您可以使用array_map
匿名函数,如下所示:
$ids = array_map(function ($ar) {return $ar['id'];}, $users);
Before(Technically php 4.0.6+), you must create an anonymous function with create_function
instead:
在(技术上为 php 4.0.6+)之前,您必须创建一个匿名函数create_function
:
$ids = array_map(create_function('$ar', 'return $ar["id"];'), $users);
回答by justnorris
PHP 5.5+
PHP 5.5+
Starting PHP5.5+ you have array_column()available to you, which makes all of the below obsolete.
从 PHP5.5+ 开始,您可以使用array_column(),这使得以下所有内容都过时了。
PHP 5.3+
PHP 5.3+
$ids = array_map(function ($ar) {return $ar['id'];}, $users);
$ids = array_map(function ($ar) {return $ar['id'];}, $users);
Solution by @phihag will work flawlessly in PHP starting from PHP 5.3.0, if you need support before that, you will need to copy that wp_list_pluck.
从 PHP 5.3.0 开始,@phihag 的解决方案将在 PHP 中完美运行,如果您在此之前需要支持,则需要复制该 wp_list_pluck。
PHP < 5.3
PHP < 5.3
WordPress 3.1+In Wordpress there is a function called wp_list_pluckIf you're using Wordpress that solves your problem.
在 Wordpress 中有一个名为wp_list_pluck的函数。 如果您使用的是 Wordpress,它可以解决您的问题。
PHP < 5.3If you're not using Wordpress, since the code is open source you can copy paste the code in your project (and rename the function to something you prefer, like array_pick). View source here
如果您不使用 Wordpress,由于代码是开源的,您可以将代码复制粘贴到您的项目中(并将函数重命名为您喜欢的名称,例如 array_pick)。在此处查看源代码
回答by deceze
If id
is the first key in the array, this'll do:
如果id
是数组中的第一个键,则执行以下操作:
$ids = array_map('current', $users);
You should not necessarily rely on this though. :)
不过,您不一定要依赖于此。:)
回答by Muraguri E
You can also use array_reduce()
if you prefer a more functional approach
array_reduce()
如果您更喜欢更实用的方法,也可以使用
For instance:
例如:
$userNames = array_reduce($users, function ($carry, $user) {
array_push($carry, $user['name']);
return $carry;
}, []);
Or if you like to be fancy,
或者,如果你喜欢花哨,
$userNames = [];
array_map(function ($user) use (&$userNames){
$userNames[]=$user['name'];
}, $users);
This and all the methods above do loop behind the scenes though ;)
这和上面的所有方法都在幕后循环;)