Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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;
    }

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

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}`;
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading