PHP 数组映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5108293/
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-25 16:50:54  来源:igfitidea点击:

PHP array mapping

phparraysmultidimensional-arraymapping

提问by powtac

Is there a cleaner way than foreachto get an array of all "label" values?

有没有比foreach获取所有“标签”值的数组更清洁的方法?

$methods[0]['label'] = 'test';
$methods[0]['nr']    = 99;
$methods[1]['label'] = 'whatever';
$methods[1]['nr']    = 10;


foreach($methods as $method) {
    $methodsLabel[] = $method['label'];
}

采纳答案by autonymous

As of PHP 5.5+, this is exactly what array_column()does:

从 PHP 5.5+ 开始,这正是array_column()它的作用:

$methodsLabel = array_column($methods, 'label');

http://php.net/manual/function.array-column.php

http://php.net/manual/function.array-column.php



3v4l example: https://3v4l.org/ZabAb

3v4l 示例:https://3v4l.org/ZabAb

回答by ircmaxell

No, there is no faster way than your implemented code. All other methods will be slower due to the overhead of a function call. For a small array the difference will be trivial, but for a large one (100 members or so, depending on implementation), the difference can be huge...

不,没有比您实现的代码更快的方法了。由于函数调用的开销,所有其他方法都会变慢。对于一个小数组,差异将是微不足道的,但对于一个大数组(100 个左右,取决于实现),差异可能很大......

You could array_mapit, but I'd stick with the raw PHP you posted above... It's easier to maintain and IMHO more readable...

你可以array_map,但我会坚持你在上面发布的原始 PHP ......它更容易维护,恕我直言更具可读性......

After all, tell me which at a glancetells you what it does more:

毕竟,告诉我哪个一目了然告诉您它的功能更多:

$results = array();
foreach ($array as $value) {
    $results[] = $value['title'];
}

vs

对比

$results = array_map(function($element) {
        return $element['title'];
    },
    $array
);

Or:

或者:

$callback = function($element) {
    return $element['title'];
}
$results = array_map($callback, $array);

Personally, the first does it for me the best. It's immediately obvious without knowing anythingwhat it's doing. The others require knowledge of array_mapsemantics to understand. Couple that with the fact that array_mapisslower, and it's a double win for foreach.

就个人而言,第一个对我来说是最好的。这是不知道显而易见东西它在做什么。其他需要array_map语义知识才能理解。再加array_map速度较慢的事实,对于foreach.

Code should only be as elegant as necessary. It should be readable above all else...

代码应该只在必要时尽可能优雅。它应该比其他一切都可读......

回答by Jacob Relkin

Sure, use array_map:

当然,使用array_map

function getLabelFromMethod($method) {
   return $method['label'];
}

$labels = array_map('getLabelFromMethod', $methods);

If you are on PHP 5.3+, you can also use a lambda function:

如果您使用的是 PHP 5.3+,您还可以使用 lambda 函数:

$labels = array_map(function($m) {
   return $m['label'];
}, $methods);

回答by Gaurav

array_map('array_shift', $methods);

Here assumption is that label will be the first element of each array.

这里假设标签将是每个数组的第一个元素。

回答by efritz

On PHP 5.3+ you can use an anonymous function paired with array_map.

在 PHP 5.3+ 上,您可以使用与 array_map 配对的匿名函数。

$methodsLabel = array_map(function($item) { return $item['label']; }, $methods);

回答by Ravi Hirani

If label is first element in array then "current" with array_mapwill work fine.

如果 label 是数组中的第一个元素,则带有array_map 的“当前”将正常工作。

array_map('current', $methods); 

回答by Gary Thomas

Arrow functionshave been introduced into PHP 7.4, which makes this a little bit cleaner.

PHP 7.4 中引入了箭头函数,这使得它更简洁一些。

$methodsLabel = array_map(fn($x) => $x['label'], $methods)