472,352 Members | 1,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,352 software developers and data experts.

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 12753
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,658 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

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

Similar topics

1
by: Doug C via .NET 247 | last post by:
Using C#... I am pulling shared memory in to my app that is in the form of apredefined structure. I have arrays in 2 sub-structures. Onearray is...
3
by: Klaus Rudolph | last post by:
Hi all, I have 2 classes, the second one should include an array of the first on. The first class should not have a default constructor. How...
8
by: Josh Lessard | last post by:
Given a union definition: union problem_t { int mask; struct { int indices; int ops; } comp; };
5
by: pmatos | last post by:
Hi all, I have a vector of vector of ints, I could use C approach by using int but I think C++ vector<vector<int> > would be easier to manage....
12
by: jimmij | last post by:
Hi, Please look at the code bellow /*******************/ class ctab { private: static const unsigned n=48;
4
by: jayharris | last post by:
I'm having a ton of trouble initializing a multi-dimensional array inside a constructor, largely because I don't know the size of the array until...
11
by: sg71.cherub | last post by:
Hi All, I have encapsulate CvMat of OpenCV into my own matrix class as the following: class CVMatrix { //== Fields private: unsigned...
13
by: John | last post by:
Is this a valid C++ program that will not crash on any machine? #include <iostream> using namespace std; int main( void ) { int i; cin >i;...
12
by: Mik0b0 | last post by:
Hallo. Let's say, there is a structure struct struct10{ int a1; int a2; int a3; int a4; }count={ {10,20,30,40}, {50,60,70,80}
4
by: Peskov Dmitry | last post by:
class simple_class { int data; public: simple_class() {data=10;}; simple_class(int val) : data(val){} }; int main() {
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.