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

Defining large array?

P: n/a
Hello,

I tried to create an array with 1000 cells, keys 0 thru 999 using

$myarr = array (1000);

But this leads to an array of 1 cell with value 1000;
Now I have a workable solution

for ($i=0 ; $i < 1000; $i++) $myarr [$i] = 0;

And in the PHP manual online I found someones' solution using array_pad ();

But I would be surprised if there isn't a simpler, standard way of defining
an array of a given size.
In Pascal I am used to var myarr : array [0..999] of anytype;
Is there something similar in PHP ?
TIA,
Pjotr
Jul 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Pjotr Wedersteers <x3****@westerterp.com> wrote:
In Pascal I am used to var myarr : array [0..999] of anytype;
Is there something similar in PHP ?


Why would you need this? Arrays are dynamic... you can create any
key/index and any time in the programs execution (if you have enough
memory availabe). Creating an empty (fixed) size array seems kind of
useless...

--

Daniel Tryba

Jul 17 '05 #2

P: n/a
On Sat, 24 Jul 2004 11:27:31 +0000 (UTC), Daniel Tryba
<ne****************@canopus.nl> wrote:
Pjotr Wedersteers <x3****@westerterp.com> wrote:
In Pascal I am used to var myarr : array [0..999] of anytype;
Is there something similar in PHP ?
Why would you need this?


You read the question. Why do you ask such a silly thing?
Arrays are dynamic... you can create any
key/index and any time in the programs execution (if you have enough
memory availabe).
You answered the question almost correctly then...

Creating an empty (fixed) size array seems kind of useless...


You went back to dumbsville? He started off by saying "In
Pascal....". Pascal doesn't HAVE dynamic arrays. You have to define
them. if you set var myarr:array[10] of char(10), for example.
You'd have:

[1]:[NULL]
[2]:[NULL]
[3]:[NULL]
[4]:[NULL]
[5]:[NULL]
[6]:[NULL]
[7]:[NULL]
[8]:[NULL]
[9]:[NULL]
[10]:[NULL]

So myvar[3] would return null until you set a value. That's not an
empty array.
Seems your added remarks at the beginning and the end were just to
show you are a jerk.


--
gburnore@databasix dot com
---------------------------------------------------------------------------
How you look depends on where you go.
---------------------------------------------------------------------------
Gary L. Burnore | ۳ݳ޳ݳۺݳ޳ݳݳ޳ݳ۳
| ۳ݳ޳ݳۺݳ޳ݳݳ޳ݳ۳
DataBasix | ۳ݳ޳ݳۺݳ޳ݳݳ޳ݳ۳
| ۳ 3 4 1 4 2 ݳ޳ 6 9 0 6 9 ۳
Black Helicopter Repair Svcs Division | Official Proof of Purchase
================================================== =========================
Want one? GET one! http://signup.databasix.com
================================================== =========================
Jul 17 '05 #3

P: n/a
Gary L. Burnore <gb******@databasix.com> wrote:
In Pascal I am used to var myarr : array [0..999] of anytype;
Is there something similar in PHP ?
Why would you need this?


You read the question. Why do you ask such a silly thing?


Because the answer to my question isn't in the OPs question.
Arrays are dynamic... you can create any
key/index and any time in the programs execution (if you have enough
memory availabe).


You answered the question almost correctly then...


I didn't answer the question at all, I just told it isn't very useful to
create an array like the OP wants to.
Creating an empty (fixed) size array seems kind of useless...


You went back to dumbsville?


Haven't touched pascal in the last 12 years, so the answer is no.
He started off by saying "In Pascal....". Pascal doesn't HAVE dynamic
arrays. You have to define them.
This is PHP, not pascal.
if you set var myarr:array[10] of
char(10), for example. You'd have: [1]:[NULL] .... [10]:[NULL]

So myvar[3] would return null until you set a value. That's not an
empty array.
The array has a size, but all elements are empty (in the way PHP defines
empty (http://php.net/empty). Like I said before: it isn't useful, at
least in most cases.
Seems your added remarks at the beginning and the end were just to
show you are a jerk.


And the purpose of this remark is ???

--

Daniel Tryba

Jul 17 '05 #4

P: n/a
.oO(Daniel Tryba)
Pjotr Wedersteers <x3****@westerterp.com> wrote:
In Pascal I am used to var myarr : array [0..999] of anytype;
Is there something similar in PHP ?


Why would you need this? Arrays are dynamic... you can create any
key/index and any time in the programs execution (if you have enough
memory availabe). Creating an empty (fixed) size array seems kind of
useless...


Depends.

Sometimes it's useful to have an initialized array. For example a read-
access on a non-existent element causes a notice:

$myarr = array();
$myarr[42]++;

--> notice (undefined offset)

So you either have to check with isset():

$myarr = array();
isset($myarr[42]) ? $myarr[42]++ : $myarr[42] = 1;

Or simply initialize the entire array before usage:

$myarr = array_fill(0, 1000, 0);
$myarr[42]++;

Micha
Jul 17 '05 #5

P: n/a

"Pjotr Wedersteers" <x3****@westerterp.com> wrote in message
news:41*********************@news.xs4all.nl...
Hello,

I tried to create an array with 1000 cells, keys 0 thru 999 using

$myarr = array (1000);

But this leads to an array of 1 cell with value 1000;
Now I have a workable solution

for ($i=0 ; $i < 1000; $i++) $myarr [$i] = 0;

And in the PHP manual online I found someones' solution using array_pad ();
But I would be surprised if there isn't a simpler, standard way of defining an array of a given size.
In Pascal I am used to var myarr : array [0..999] of anytype;
Is there something similar in PHP ?
TIA,
Pjotr


http://www.php.net/array_fill
Jul 17 '05 #6

P: n/a
Pjotr Wedersteers wrote:
Hello,

I tried to create an array with 1000 cells, keys 0 thru 999 using

$myarr = array (1000);

But this leads to an array of 1 cell with value 1000;
Now I have a workable solution

for ($i=0 ; $i < 1000; $i++) $myarr [$i] = 0;

And in the PHP manual online I found someones' solution using
array_pad ();

But I would be surprised if there isn't a simpler, standard way of
defining an array of a given size.
In Pascal I am used to var myarr : array [0..999] of anytype;
Is there something similar in PHP ?
TIA,
Pjotr


Thanks guys, array_fill will pretty much suit my needs! I was vaguely aware
of the fact PHP can create and extend arrays dynamically, and Pascal indeed
does not allow that. Most of the times Pascal is a pain in the neck because
of that. Here I needed to be sure all cells actually exist, or later on the
required mathematical operations on the array could become a nightmare.

And indeed I do check the manual many times a day, mostly with success,
sometimes I just happen to miss the right function. Only human! So thanks a
lot for your patience, explanations and helping me out in general.
Pjotr
Jul 17 '05 #7

P: n/a
"Pjotr Wedersteers" wrote:
Pjotr Wedersteers wrote:
Hello,

I tried to create an array with 1000 cells, keys 0 thru 999 using

$myarr = array (1000);

But this leads to an array of 1 cell with value 1000;
Now I have a workable solution

for ($i=0 ; $i < 1000; $i++) $myarr [$i] = 0;

And in the PHP manual online I found someones solution using
array_pad ();

But I would be surprised if there isnt a simpler,

standard way of
defining an array of a given size.
In Pascal I am used to var myarr : array [0..999] of anytype;
Is there something similar in PHP ?
TIA,
Pjotr


Thanks guys, array_fill will pretty much suit my needs! I was

vaguely aware
of the fact PHP can create and extend arrays dynamically, and Pascal indeed
does not allow that. Most of the times Pascal is a pain in the neck
because
of that. Here I needed to be sure all cells actually exist, or later on the
required mathematical operations on the array could become a
nightmare.

And indeed I do check the manual many times a day, mostly with
success,
sometimes I just happen to miss the right function. Only human! So
thanks a
lot for your patience, explanations and helping me out in general.
Pjotr


Pjotr, just to make things clear (just in case) $myarr = array (1000);
is actually populating the array with the first value being 1000 (as
you have found out).

Regarding Pascal, etc. I think these languages were invented at the
time when memory and cpu were expensive. So they want you to define
things very exactly, as e.g. dynamically expanding arrays on the run
is "expensive". PHP does all the hard work in the background so the
programmer does not have to. This works in the age of cheap memory
and cpu.

--
http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/PHP-Defining...ict132884.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=443859
Jul 17 '05 #8

P: n/a
.oO(steve)
Regarding Pascal, etc. I think these languages were invented at the
time when memory and cpu were expensive.
You can't compare a strictly typed language like Pascal with a scripting
language like PHP. Of course in Pascal you have to exactly declare what
you want, of what type a variable should be and how many items an array
should have, you have to take care of your objects and destroy them if
they are not needed anymore etc., that's simply the way such languages
work and has it's advantages and drawbacks (BTW recent Pascal compilers
like Delphi and FPC also allow dynamic arrays).

On the other hand PHP was developed so that developers don't have to
care about such things, because it doesn't really matter in scripts.
IMHO scripting languages are a further abstraction of what strictly
typed languages already are, with their own benefits and problems.
So they want you to define
things very exactly, as e.g. dynamically expanding arrays on the run
is "expensive". PHP does all the hard work in the background so the
programmer does not have to. This works in the age of cheap memory
and cpu.


Arrays in PHP have nearly nothing in common with arrays in Pascal
(besides the numeric indizes). PHP's arrays are hashtables, if you like
you can implement the same in Pascal or any other language ...

Micha
Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.