473,327 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Defining large array?

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
8 3904
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
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
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
.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

"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
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
"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 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


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
.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Xah Lee | last post by:
© # the following is a example of defining © # a function in Python. © © def fib(n): © """This prints n terms of a sequence © where each term is the sum of previous two, © starting...
7
by: Bob Rock | last post by:
Hello, this may seem a strange question, but is there a way of being able to call methods of a class through an array of that class when not referencing a specific object in the array. In other...
2
by: Developwebsites | last post by:
const int MAX=999; class person { protected: char firstname, lastname; int ID; public: person();
5
by: apm | last post by:
Any and all: Is there an efficient way to pass a large array from .NET to COM? Can references (or pointer) be passed from COM to NET and NET to COM without the object it refers to being copied?...
10
by: nambissan.nisha | last post by:
I am facing this problem.... I have to define a structure at runtime as the user specifies... The user will tell the number of fields,the actual fields...(maybe basic or array types or...
7
by: ultr | last post by:
I need a large 3D array of structures: struct s { char a; int b; }; s s_array; s_array is declared as global.
2
by: Helpful person | last post by:
I wish to access several pictures on my page by defining them as an array. This way I can either loop through them or access them by array index. I am a beginner at Javascript so please keep...
10
by: Peter Duniho | last post by:
This is kind of a question about C# and kind of one about the framework. Hopefully, there's an answer in there somewhere. :) I'm curious about the status of 32-bit vs 64-bit in C# and the...
2
by: =?Utf-8?B?Z2FkeWE=?= | last post by:
I use one of 2 arrays dependent on the country. Rather than say: if exchangeID = 1 then dim myPlaceBets() as As UK.exchange.PlaceBets many statements myPlaceBetsReq.bets = myPlaceBets else...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.