Вопрос

I'm writing a little script to create a sprite from a directory full of icons. To do so I am counting the icons ($i++) in order to place them on a grid, and then return their positions.

if you can imagine a grid like this where each integer represents an icon:

 1   2   3   4   5   6   7   8   9  10
11  12  13  14  15  16  17  18  19  20
21  22  23  24  25  26  27  28  29  30
31  32  33 etc

so if $i == 14 it will be placed into row 2 in column 4, and if $i == 29 it will be in row 3 and column 9.

What I'm stuck on is how to process the number so that i can return only the units (1,2,3,4,5,6,7,8,9) for the columns, and the rows (0*,1*,2*,3*,4*,5*,6* etc).

for example:

$i == 678;
$i_x = // Whatever i need to do to get '8'
$i_y = // Whatever I need to do to '67'

Thanks :-)

Это было полезно?

Решение

You probably mean $i=678 rather than $i==678 You can use division, and modulus operations.

$i = 678;
$i_x = $i %10;
$i_y = (int)$i/10;
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top