1

Possible Duplicate:
PHP: fill an array with numbers

I'd like code a PHP function that creates an array with natural numbers from 1 to a given number and I have not a clue of how to do this.

e.g : if I give the function the natural number 3 then the array should be 1,2,3 ; if I give the function the integer 2 1,2 ; if 5 1,2,3,4,5

3
  • as the key or the value? Commented Sep 19, 2011 at 8:36
  • Please look at the list of array functions in the PHP Manual before asking superfluous questions. It's a completely pointless and worthless exercise to have people link you to the appropriate manual pages here. Also see blog.stackoverflow.com/2011/02/are-some-questions-too-simple Commented Sep 19, 2011 at 8:39
  • 1
    @webbiedave You're right webbiedave I've voted to close my question. Commented Sep 19, 2011 at 8:39

3 Answers 3

10

There is already a function that will do this: range.

range(1, 3); // array(1, 2, 3)
range(2, 5); // array(2, 3, 4, 5)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I'd never heard of this function !
2

Use range() :

$array = range(1, 3);

Comments

1

Just look at this http://ua.php.net/manual/en/function.range.php

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.