5

Asked already a question about converting functions from Ruby to JS, and now I'm trying to implement with JS to PHP, but something does not work, tell me what I'm missing?

The code on JS:

function transpose(a) {
    return a.length === 0 ? a : a[0].map((col, i) => a.map((row) => row[i]))
}

function f(a) {
    return a.length === 0 ? [] : [...a.shift(), ...f(transpose(a).reverse())];
} 

console.log(f([[1, 2, 3, 4], [12, 13, 14, 5], [11, 16, 15, 6], [10, 9, 8, 7]]))

In PHP do so:

function transpose($array) 
{
    if (count($array) === 0) return $array;

    foreach ($array as  $rowkey => $row)
    {
        foreach($row as $colkey => $col)
        {
            $out[$colkey][$rowkey] = $col;
        }
    }

    return $out;
}

function f($a)
{
    return (count($a) === 0) 
    ? [] 
    : [
        array_shift(...$a), 
        f(array_reverse(transpose(...$a)))
    ];
}

print_r(f([[1,2,3,4],[10,11,12,5],[9,8,7,6]]));

Is there a problem? Errors:

Warning: array_shift() expects exactly 1 parameter, 3 given in [...][...] on line 24

Invalid argument supplied for foreach() in [...][...] on line 10

Warning: Invalid argument supplied for foreach() in [...][...] on line 10

Warning: Invalid argument supplied for foreach() in [...][...] on line 10

Warning: Invalid argument supplied for foreach() in [...][...] on line 10

Notice: Undefined variable: out in [...][...] on line 16

Warning: array_reverse() expects parameter 1 to be array, null given in [...][...] on line 25 Warning: count(): Parameter must be an array or an object that implements Countable in [...][...] on line 21

4
  • 1
    Can you please add some more details about what you mean by "something does not work"? Commented Oct 22, 2018 at 21:25
  • You can't do that. As PHP is precompiled and JS is post compiled, you cannot do PHP from the JS side. Commented Oct 22, 2018 at 21:29
  • array_shift(...$a) should be ...array_shift($a) to be equivalent to the JS. Commented Oct 22, 2018 at 21:30
  • @Forbs, so it's not possible? Commented Oct 22, 2018 at 21:30

3 Answers 3

4

You don't need to use the ... operator in the PHP version. You just need to merge the shifted row with the result of the recursive call.

function f($a)
{
    return (count($a) === 0) 
    ? [] 
    : array_merge(
        array_shift($a), 
        f(array_reverse(transpose($a)))
    );
}

array_merge(array_shift($a), f(array_reverse(transpose($a))))

is the same thing that's happening here:

[...a.shift(), ...f(transpose(a).reverse())]

... expands the elements of the array in JS. It works similarly in PHP, but here it is only valid in function definitions or calls.

Sign up to request clarification or add additional context in comments.

Comments

3

You have your ... (AKA "splat") operators in a different place in the PHP than the JS.

Also, prior to PHP 7.4 the splat operator can't be used in array literals, only in function calls. The use in the f() function is equivalent to array_merge().

function f($a)
{
    return (count($a) === 0) 
    ? [] 
    : array_merge(
        array_shift($a), 
        f(array_reverse(transpose($a)))
    );
}

DEMO

4 Comments

Parse error: syntax error, unexpected '...' (T_ELLIPSIS)
You can't use ... in array literals, so I changed to a function call.
php 7.4 now supports unpacking inside arrays. php.net/manual/en/migration74.new-features.php
Thanks, I've added the caveat about 7.4 to the question.
1

Your array_shift has too many parameters .. All you need is the $a Same with array_reverse

<?php
function transpose($array)
{
    if (count($array) === 0) return $array;

    foreach ($array as  $rowkey => $row)
    {
        foreach($row as $colkey => $col)
        {
            $out[$colkey][$rowkey] = $col;
        }
    }

    return $out;
}

function f($a)
{
    return (count($a) === 0)
    ? []
    : [
        array_shift($a),
        f(array_reverse(transpose($a)))
    ];
}

print_r(f([[1,2,3,4],[10,11,12,5],[9,8,7,6]]));

RESULT

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
        )

    [1] => Array
        (
            [0] => Array
                (
                    [0] => 5
                    [1] => 6
                )

            [1] => Array
                (
                    [0] => Array
                        (
                            [0] => 7
                            [1] => 8
                            [2] => 9
                        )

                    [1] => Array
                        (
                            [0] => Array
                                (
                                    [0] => 10
                                )

                            [1] => Array
                                (
                                    [0] => Array
                                        (
                                            [0] => 11
                                            [1] => 12
                                        )

                                    [1] => Array
                                        (
                                        )

                                )

                        )

                )

        )

)

2 Comments

This result isn't the same as the JavaScript version.
It's supposed to return a flat array, not heavily nested.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.