Page 1 of 1

Setting predefined array length?

Posted: Thu Feb 02, 2006 5:59 pm
by cj5
Can you set an array's length, without actually populating it, or manually entering the indices.

Code: Select all

<?php $array = array(); ?>
that's count($array) = 0

but i need something similar to JavaScript, where you can do this:

Code: Select all

var ary = new Array(5);
that's ary.length() = 5

any ideas?

Posted: Thu Feb 02, 2006 6:07 pm
by John Cartwright
No, I don't know so.. although I'm not positive..

Why would you need to?

NEVERMIND

Posted: Thu Feb 02, 2006 6:08 pm
by cj5
Sorry, nevermind. Found it in the manual: http://www.php.net/manual/en/function.array.php#4806

Posted: Thu Feb 02, 2006 6:12 pm
by cj5
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.

Posted: Thu Feb 02, 2006 6:48 pm
by raghavan20
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...

Posted: Thu Feb 02, 2006 6:58 pm
by Jenk

Code: Select all

<?php
$ar = array_pad(array(), 5, null);
print_r($ar);
?>
outputs:

Code: Select all

Array
(
    [0] => 
    [1] => 
    [2] => 
    [3] => 
    [4] => 
)
as an example.

Posted: Thu Feb 02, 2006 7:02 pm
by josh
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.

Posted: Thu Feb 02, 2006 7:28 pm
by Jenk

Code: Select all

<?php
$ar = array_pad(array(), 5, null);
print_r($ar);

var_dump(count($ar));
?>

Code: Select all

Array
(
    [0] => 
    [1] => 
    [2] => 
    [3] => 
    [4] => 
)
int(5)
Achieves what the OP asked for..

Posted: Thu Feb 02, 2006 7:37 pm
by John Cartwright
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?

Posted: Thu Feb 02, 2006 8:08 pm
by josh
Exactly, we should point out the other, [possibly] better ways to skin the cat so to speak

Posted: Fri Feb 03, 2006 5:41 am
by Jenk
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. :)

The code is ALIVE!

Posted: Fri Feb 03, 2006 7:44 am
by cj5
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