Search code examples
phptypescripttranslate

TypeScript oneliner to PHP


I'm translating a code from TypeScript to PHP and that's going well so far. In TypeScript there are some oneliners that I need to translate but I don't seem to get it right.

The TypeScript code is:

const InContent: string = subclass.Ins
        .map((In: In) => In.OutId + In.OutIndex)
        .reduce((a, b) => a + b, '');

Now I know that PHP 7 has functions like array_map, array_reduce and array_column. I think it is possible to create this TypeScript oneliner also in PHP as a oneliner, but I can't see how.


Solution

  • This code appears to add the OutId and OutIndex together (I can only assume it's a mathematical addition, and not a string concatenation), and then concatenate all those together into one string. So:

    join(array_map(function ($in) { return $in->outId + $in->outIndex; }, $ins))
    // I'm assuming `$in` is an object here
    

    Even in JS that reduce would be more succinct as map(...).join().