Generate a set of numbers in php

Asked

Viewed 95 times

-1

Friends need to automatically generate a sequence of numbers and comma separations in php, for example. 1,2,3,4,5,6,7,8,9,10,11,12 save in a variable and then make an Insert in a mysql database.

sort of like this.

INSERT INTO table (n_serie, year, numbers)

INSERT INTO shipments );

  • 2

    Read about the functions join and range.

1 answer

1

RANGE FUNCTION() - returns a sequence according to the parameters defined by nodes.

//retorna uma matriz de elementos de "1" a "12"
$numeros = range(1,12);

outworking

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] => 10 [10] => 11 [11] => 12 )


FUNCTION Join () - returns a string of the elements of an array.

$numeros = range(1,12);

echo join(",",$numeros);

Upshot

1,2,3,4,5,6,7,8,9,10,11,12

Functional example - Ideone

  • excellent, worked out here. thank you very much.

  • @WELLINGTONLEITE, no need to thank, when any answer solve your problem mark it as accepted see how in https://i.stack.Imgur.com/evLUR.png

Browser other questions tagged

You are not signed in. Login or sign up in order to post.