By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,965 Members | 1,702 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,965 IT Pros & Developers. It's quick & easy.

.. count shortcut

P: n/a
I need to make an array of 1 to X items

in perl , this would be: 1..X .

Is there such a thing in php, or do I need to make a loop?

Jeff
Sep 9 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
..oO(Jeff)
I need to make an array of 1 to X items

in perl , this would be: 1..X .

Is there such a thing in php, or do I need to make a loop?
range() maybe?

Micha
Sep 9 '08 #2

P: n/a
Michael Fesser wrote:
.oO(Jeff)
> I need to make an array of 1 to X items

in perl , this would be: 1..X .

Is there such a thing in php, or do I need to make a loop?

range() maybe?
perfect!

Even more useful than the perl construct.

Jeff
>
Micha
Sep 9 '08 #3

P: n/a
Jeff wrote:
>> I need to make an array of 1 to X items

in perl , this would be: 1..X .

Is there such a thing in php, or do I need to make a loop?

range() maybe?

perfect!

Even more useful than the perl construct.
Just be aware that

$arr = array();
foreach (range(0,count($arr)-1) as $i) print "$i\n";

and

my @arr = ();
for my $i (0 .. $#arr) { print "$i\n"; }
behaves differently.
Sep 9 '08 #4

P: n/a
Matija Papec wrote:
Jeff wrote:
>>> I need to make an array of 1 to X items

in perl , this would be: 1..X .

Is there such a thing in php, or do I need to make a loop?

range() maybe?

perfect!

Even more useful than the perl construct.

Just be aware that

$arr = array();
foreach (range(0,count($arr)-1) as $i) print "$i\n";

and

my @arr = ();
for my $i (0 .. $#arr) { print "$i\n"; }
behaves differently.
They do behave the same (ran each at the command line to be sure).
Maybe you didn't mean to subtract 1 from count($arr), in the PHP example.

These two are the same:

PHP
$indexes = range(0, count($arr)-1);

Perl
my @indexes = (0..$#arr);

So are these:

PHP
$elements = range(0, count($arr));

Perl
my @elements = (0 .. scalar @arr);

--
Curtis
Sep 10 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.