Skip to content
Advertisement

PHP – 2D array to string

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.

Advertisement

Answer

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));
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement