英文:
Why the extra implode, array_map, etc functions instead of returning simply a string from the start? Trying to move this PHP to node
问题
private static function getOrdering($sortingColumn, $sortingDirection)
{
if ($sortingColumn === 'reportTime') {
return implode(', ', array_map(function ($column) use ($sortingDirection) {
return $column . ' ' . $sortingDirection;
}, ['report_date', 'report_hour', 'report_minute']));
}
return $sortingColumn . ' ' . $sortingDirection;
}
In the provided code, there is a function called getOrdering
that takes two parameters: $sortingColumn
and $sortingDirection
. It returns a string that represents the sorting order based on these parameters.
If $sortingColumn
is equal to 'reportTime', it enters the conditional block and does the following:
-
It uses
array_map
to apply an anonymous function to each element in the array['report_date', 'report_hour', 'report_minute']
. This function takes each column name as$column
and combines it with the$sortingDirection
. This creates an array of strings like 'report_date DESC', 'report_hour DESC', etc. -
The
implode
function is then used to concatenate these strings with ', ' as the separator, resulting in a single string where the elements are joined by a comma and a space.
If $sortingColumn
is not 'reportTime', it simply concatenates $sortingColumn
and $sortingDirection
with a space in between.
Regarding your second question about why not directly returning the appropriate string, the reason for using array_map
and implode
in this case is to make the code more flexible and maintainable. If you ever need to change the columns or the logic for combining them with the sorting direction, you can do so easily in one place (inside array_map
) without modifying the rest of the code. It provides a level of abstraction and reusability, which can be valuable in more complex scenarios or when the code may evolve over time.
英文:
private static function getOrdering($sortingColumn, $sortingDirection)
{
if ($sortingColumn === 'reportTime') {
return implode(', ', array_map(function ($column) use ($sortingDirection) {
return $column . ' ' . $sortingDirection;
}, ['report_date', 'report_hour', 'report_minute']));
}
return $sortingColumn . ' ' . $sortingDirection;
}
I'm struggling a bit to understand how the combination of implode and array_map are working. Moreso, what exactly does array_map(function ($column) use ($sortingDirection)...
mean? The function ($column) (what does this mean, and where does column come from?) is throwing me off a bit. I'm quite new to PHP, so any basic explanation will likely help.
Edit: Apologies. This is far too broad. Perhaps a more pointed question is:
I did try something, but I'm wondering why in the case that the sortingColumn is "reportTime", we don't simply return a concatenation of the "report_date", "report_hour", etc along with the sortingDirection appended to each. Running:
$sortingDirection = 'DESC';
echo(implode(' , ', array_map(function ($column) use ($sortingDirection) { return $column . ' ' . $sortingDirection;}, ['report_date', 'report_hour', 'report_minute'])));
gives me what I'd think: report_date DESC, report_hour DESC, etc. Why go through the extra steps of implode, array_map, etc when I could just return the appropriate string right from the start?
答案1
得分: 2
我认为PHP的匿名函数的文档应该会有帮助。
PHP函数具有函数范围(不像JavaScript的块范围)。use
关键字用于将外部作用域的变量引入匿名函数。
array_map()
与JS中的Array.prototype.map()
非常相似。它接受一个回调函数,用于对数组中的每个元素(在你的示例中是$column
)执行操作。
JavaScript的等效代码可能如下所示...
const getOrdering = (sortingColumn, sortingDirection) => {
if (sortingColumn === "reportTime") {
return ["report_date", "report_hour", "report_minute"]
.map((column) => `${column} ${sortingDirection}`) // array_map()
.join(", "); // implode()
}
return `${sortingColumn} ${sortingDirection}`;
};
你肯定可以返回一个包含$sortingDirection
插值的字符串,例如
return "report_date $sortingDirection, report_hour $sortingDirection, ...";
但问题在于可维护性。添加、删除或通常更改列表会变得繁琐,而维护一个数组则简单得多。甚至可以将其存储在其他地方,比如配置文件或数据库中。
英文:
I think the docs for PHP's anonymous functions should help.
PHP functions have function-scope (not block scope like JavaScript). The use
keyword is used to pull in outer scoped variables into the anonymous function.
array_map()
is very similar to Array.prototype.map()
in JS. It accepts a callback function to execute for each element in the array ($column
in your example).
The JavaScript equivalent would look something like this...
const getOrdering = (sortingColumn, sortingDirection) => {
if (sortingColumn === "reportTime") {
return ["report_date", "report_hour", "report_minute"]
.map((column) => `${column} ${sortingDirection}`) // array_map()
.join(", "); // implode()
}
return `${sortingColumn} ${sortingDirection}`;
};
You could definitely return a string with $sortingDirection
interpolated, eg
return "report_date $sortingDirection, report_hour $sortingDirection, ...";
but the issue then is maintainability. Adding, removing or generally altering the list becomes a chore whereas maintaining an array is much simpler. It could even be stored elsewhere say in a configuration file or database.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论