469,271 Members | 1,466 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

initializing 2d arrays in php

34
How do i initialize a 2d array in php,
if i use the 2d array in a class declaration.

Expand|Select|Wrap|Line Numbers
  1.  
  2. class ANIMALS {
  3.   var $array[ ] [ ] = 0;    //?? how do I initialize the array if I don't know what the size is going to be...
  4.  
  5. .....
  6. }
  7.  
since I don't know how big size is going to be, ie...how many rows...
How do I initialize it? say array was rows x 5 (columns)... rows is supposed to be determined on the fly...depending on how many records get fetched from the database. How do I initialize this array?

Thanks.
Mar 11 '09 #1
8 12608
TheServant
1,168 Expert 1GB
I don't know what you mean by initialize it? Just declare your arrays:
Expand|Select|Wrap|Line Numbers
  1. $array['var1']['name'] = "var1";
  2. $array['var1']['value'] = 66;
  3. $array['var2']['name'] = "var2";
  4. $array['var2']['value'] = 154;
  5. $array['var3']['name'] = "var3";
  6. ...
What have you tried? Are you getting an error? How are you fetching from the database?
Mar 11 '09 #2
gsherp
34
Don't I need to initialize it.....like set it to the value of 0. In case someone tries to access that array before it is ready. I don't want it to return a null or some random value.

You declare the array name. The size is unknown. How do you initialize it without putting the array in a loop to set all the values at all the indexes to 0?

hope that was clear.
Mar 11 '09 #3
TheServant
1,168 Expert 1GB
You can look up the function "array_fill()", and then combine it with count() - which finds the length and I am sure you can come up with a solution. Check out all the array functions if you haven't already.
Mar 11 '09 #4
Dormilich
8,651 Expert Mod 8TB
you don't need to initialize. to check if you have an array at all, use is_array(). you can also set an empty array by
Expand|Select|Wrap|Line Numbers
  1. $a = array();
Mar 11 '09 #5
gsherp
34
I guess it goes down to this question....does one need to initialize a variable in php before using it?


do i have to do this:
Expand|Select|Wrap|Line Numbers
  1. $variable = 0;
  2. ....
  3. $variable = $variable2 + $variable3;
  4.  
  5.  
or can I just forgo the $variable=0; statement?

Thanks for all the help
Mar 11 '09 #6
TheServant
1,168 Expert 1GB
No, there is no initialization/declaration required in PHP. Just define it with no declaration. No parameter definitions (content type or size) are required.
So if $variable2 and $variable3 already have values, then you can make $variable equal to them added, you don't need to pre define $variable.

Hope that helps.
Mar 11 '09 #7
gsherp
34
Yeah...that is good. Thanks for all the help
Mar 11 '09 #8
TheServant
1,168 Expert 1GB
No worries. Hope to see you around sometime.
Mar 11 '09 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Doug C via .NET 247 | last post: by
3 posts views Thread by Klaus Rudolph | last post: by
8 posts views Thread by Josh Lessard | last post: by
12 posts views Thread by jimmij | last post: by
13 posts views Thread by John | last post: by
4 posts views Thread by Peskov Dmitry | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.