PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Jcart wrote:No, I don't know think.. although I'm not positive..
Why would you need to?
Well, it's a top secret reason....
Just kidding, no, really, it's because I need to establish a predefined set of elements, and there needs to be 18 placeholders in that array. A lot of other languages use it like C++, Java, ColdFusion, and JavaScript, so I figured PHP would too. It just makes coder life easier, that's why.
I still could that information you gave in the link. Can you write down the syntax and I am curious to know it...
Mostly arrays with undefined sizes are considered to be a luxury because in most situations you need dynamic arrays and in other languages I always have to google to find out how to declare dynamic arrays.
I do understand that in some cases like stack or elsewhere you might want to force a limit on number of elements in the data structure but you can also control it by having a function that adds elements to the array and that function checks for valid number of elements in the array before it adds one...
But jenk, wether or not you pad it $array[4] is going to give you the same return value. If the reason is so you can enter a foreach loop on the array that can be solved by using a for loop... just loop over the array a pre-defined number of times. OP - even if your array is emtpy you can still assing brand new values in the array operator $array[4]='test'; the array will just have one element after that call.
Jenk, now and then the OP won't completely understand the situation, and assume that his way is the best way. If we didn't question methods we'd still be using punch card computers.
That is why we discuss things, as I am interested in why this would be needed?
jshpro2 wrote:But jenk, wether or not you pad it $array[4] is going to give you the same return value. If the reason is so you can enter a foreach loop on the array that can be solved by using a for loop... just loop over the array a pre-defined number of times. OP - even if your array is emtpy you can still assing brand new values in the array operator $array[4]='test'; the array will just have one element after that call.
That is a mistruth.
If you don't pad it, you create and error/exception of E_NOTICE level.
Well, I was revising a class I built to parse real-time buoy data from the NOAA website, and realized that certain buoys read from different file formats (each line of a certain format takes on different amount of columns based on what kind of buoy I am querying). So my answer to that was to rebuild the data array that is made from parsing these varying files. So, for each type, I need to find out how much space I need to fill, If I don't I get warnings and notices saying that there is no array index available
Jenk wrote:If you don't pad it, you create and error/exception of E_NOTICE level.
, so I set it up with null indices, so that no matter how small the data array is after parsing the text file, there will never warning telling my that the index does not exist. It's either PAD it or manually setup the array with all the empty values. array_pad() is awesome in that sense