Page 1 of 1
Array declarations
Posted: Tue Apr 03, 2012 5:34 am
by dacer
Hi, I used to use asp/aspx/vb code, so I'm confused with php.
I want to create an array, but I don't know how many elements it will have. I can do:
And then populate it with
This work fine, but I have this warning on server side (apache error log)
PHP Notice: Undefined offset: 0 in ..
How can I declare myArray as array, and later set so many elements as I need. For example in Vb, "redim array(5)".
Thanks
Re: Array declarations
Posted: Tue Apr 03, 2012 5:39 am
by requinix
There's something else going wrong with your code. It's not with the array - you've got the right idea.
Here's one thing I see: you don't have quotes around the "index" in that second bit of code. If that's supposed to be a string then it needs quotes.
What's your actual code? You may simply have a typo, or maybe you're misunderstanding (or not even aware) of some other PHP thing.
Re: Array declarations
Posted: Tue Apr 03, 2012 5:52 am
by dacer
Re: Array declarations
Posted: Wed Apr 04, 2012 3:14 am
by dacer
don't know if is the best way to do, but I get solved with this line, after Iget how many elements must have my array ($HowManyElements)
Code: Select all
$myArray = array_fill(0, $HowManyElements, '');
With this line I have not errors (warnings) on
/var/log/apache2/error.log
Sl2
Re: Array declarations
Posted: Wed Apr 04, 2012 3:34 am
by social_experiment
dacer wrote:I want to create an array, but I don't know how many elements it will have.
Is there a specific reason for this; the size of the array it seems will only be limited by the amount of memory that is allocated for the script
It will be easier to assist you if you can paste the code giving the issues