Setting predefined array length?

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!

Moderator: General Moderators

Post Reply
User avatar
cj5
Forum Commoner
Posts: 60
Joined: Tue Jan 17, 2006 3:38 pm
Location: Long Island, NY, USA

Setting predefined array length?

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

No, I don't know so.. although I'm not positive..

Why would you need to?
Last edited by John Cartwright on Thu Feb 02, 2006 7:35 pm, edited 1 time in total.
User avatar
cj5
Forum Commoner
Posts: 60
Joined: Tue Jan 17, 2006 3:38 pm
Location: Long Island, NY, USA

NEVERMIND

Post by cj5 »

Sorry, nevermind. Found it in the manual: http://www.php.net/manual/en/function.array.php#4806
User avatar
cj5
Forum Commoner
Posts: 60
Joined: Tue Jan 17, 2006 3:38 pm
Location: Long Island, NY, USA

Post 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.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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...
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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..
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Exactly, we should point out the other, [possibly] better ways to skin the cat so to speak
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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. :)
User avatar
cj5
Forum Commoner
Posts: 60
Joined: Tue Jan 17, 2006 3:38 pm
Location: Long Island, NY, USA

The code is ALIVE!

Post 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
Post Reply