P
P
polyvoidex2018-03-09 17:52:17
PHP
polyvoidex, 2018-03-09 17:52:17

How to break one line into several lines of the same length and put them in an array?

I am making a 2D game, like minecraft, with voxel graphics, where I store the map in a certain format. The file (map.txt) stores only block IDs, i.e. there is no division into rows and columns. There is a certain number of bytes - one cell.
I need to put the whole table into an array in order to display it on the screen. For example:
There is a string: 023174107052081607340185201210254072012807125012704195472732
The output should be like this: Array ( [0] => Array ( [0] => 0231 [1] => 7410 [2] => 7052 [3] => 0816 [4] = > 0734 ) [1] => Array ( [0] => 0185 [1] => 2012 [2] => 1025 [3] => 4072 [4] => 0128 ) [2] => Array ( [0 ] => 0712 [1] => 5012 [2] => 7041 [3] => 9547 [4] => 2732) )
In this example, one cell contains four characters and one line consists of five cells. There are thoughts that you can iterate over the string character by character, but I'm new to PHP. If it doesn't work out the way I want, then I'll have to make a CSV table.
Help me please.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
roswell, 2018-03-09
@polyvoidex

$s = '023174107052081607340185201210254072012807125012704195472732';
$f = array_map(function($a) {
    return str_split($a, 4);
}, str_split($s, 4 * 5));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question