Home > Enterprise >  PHP - 2D array to string
PHP - 2D array to string

Time:10-06

I would need this matrix represented by a 2d field

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

    [1] => Array
        (
            [0] => .
            [1] => X
            [2] => X
            [3] => .
        )

    [2] => Array
        (
            [0] => .
            [1] => X
            [2] => X
            [3] => .
        )
)

on a string in the form.

'.... \n

.XX. \n

.XX.\n

....'

Thank you in advance for any advice.

CodePudding user response:

Use array_map() to call implode() on each element to get an array of strings. Then implode that to get a single string separated by \n.

$result = implode('\n', array_map('implode', $array));
  • Related