Printing out a Matrix by Using the Implode Function in PHP
PHPIn PHP, we can use a 2D array to store data for a matrix. A 2D array is actually an array of arrays. A 2D matrix can then be created as below.
$m = array
(
array(0, 1, 2),
array(3, 4, 5),
array(6, 7, 8)
);
In PHP, the implode function implode(string $glue, array $pieces ):string
can be used to join array elements with a glue string.
It makes it easy to print out an array in PHP.
To print out the data of a 2D matrix as above, we can do like this:
for ($i = 0; $i < count($m); $i++) {
echo implode(' ', $m[$i]).'<br>';
}
The output will be:
0 1 2
3 4 5
6 7 8