473,544 Members | 263 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

initializing 2d arrays in php

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

Expand|Select|Wrap|Line Numbers
  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...
  5. .....
  6. }
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?

Mar 11 '09 #1
8 12862
1,168 Recognized Expert Top Contributor
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
34 New Member
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
1,168 Recognized Expert Top Contributor
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
8,658 Recognized Expert Moderator Expert
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
34 New Member
I guess it goes down to this question....doe s 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;
or can I just forgo the $variable=0; statement?

Thanks for all the help
Mar 11 '09 #6
1,168 Recognized Expert Top Contributor
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
34 New Member
Yeah...that is good. Thanks for all the help
Mar 11 '09 #8
1,168 Recognized Expert Top Contributor
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

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 an array of another predefined structure, and the otheris simply an array of ushort's. Such as: public struct predefinedStruct { public ushort a; public ushort b; } public struct...
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 could I use an array of the first classes? If it is impossible to initialize the array before entering the constructor , it would never be possible to use arrays of classes in C++ whitout using a...
by: Josh Lessard | last post by:
Given a union definition: union problem_t { int mask; struct { int indices; int ops; } comp; };
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. So I have a function which creates and initializes the vector with the values I need (I know these values before hand). - What's the best way to initialize the vector<vector<int> >? Can I...
by: jimmij | last post by:
Hi, Please look at the code bellow /*******************/ class ctab { private: static const unsigned n=48;
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 runtime. I have a class that looks like this: class MyClass { public: const int size; MyClass( const int ); };
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 m_Width;
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; double X; X = 1123;
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}
by: Peskov Dmitry | last post by:
class simple_class { int data; public: simple_class() {data=10;}; simple_class(int val) : data(val){} }; int main() {
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.