-2
    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?

Phil
  • 157,677
  • 23
  • 242
  • 245
kastaplastislife
  • 293
  • 2
  • 16
  • Why don't you try it and see? https://3v4l.org/PNX1A – WOUNDEDStevenJones Jan 10 '23 at 04:16
  • 1
    While the dv's on this question may be merited. This question seems narrow enough to keep open. [How to deal with questions of the type "I don't understand how this code works"?](https://meta.stackoverflow.com/q/278797/2943403) – mickmackusa Jan 10 '23 at 04:25
  • 1
    If you want to avoid `implode(array_map())`, then `array_reduce()` can be used. I agree with Phil, though, it is simpler to read an imploded array versus a multi-concatenated string building process. – mickmackusa Jan 10 '23 at 04:59
  • Thanks for the clarification folks. That clears up everything I was curious. I hope my edits to the post made the question more clear if others come across this in the future. – kastaplastislife Jan 10 '23 at 05:02
  • My opinion is that the the extension of this question's focus has made more work for Phil's answer. It's kind of unclear if you are seeking php advice or node advice. I'm not sure why we're talking about node at all. – mickmackusa Jan 10 '23 at 05:05

1 Answers1

2

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.

Phil
  • 157,677
  • 23
  • 242
  • 245