initializing 2d arrays in php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gsherp
    New Member
    • May 2007
    • 34

    initializing 2d arrays in php

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

    Code:
     
    class ANIMALS {
      var $array[ ] [ ] = 0;    //?? how do I initialize the array if I don't know what the size is going to be...
    
    .....
    }
    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.
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    #2
    I don't know what you mean by initialize it? Just declare your arrays:
    Code:
    $array['var1']['name'] = "var1";
    $array['var1']['value'] = 66;
    $array['var2']['name'] = "var2";
    $array['var2']['value'] = 154;
    $array['var3']['name'] = "var3";
    ...
    What have you tried? Are you getting an error? How are you fetching from the database?

    Comment

    • gsherp
      New Member
      • May 2007
      • 34

      #3
      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.

      Comment

      • TheServant
        Recognized Expert Top Contributor
        • Feb 2008
        • 1168

        #4
        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.

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          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
          Code:
          $a = array();

          Comment

          • gsherp
            New Member
            • May 2007
            • 34

            #6
            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:
            Code:
            $variable = 0;
            ....
            $variable = $variable2 + $variable3;
            or can I just forgo the $variable=0; statement?

            Thanks for all the help

            Comment

            • TheServant
              Recognized Expert Top Contributor
              • Feb 2008
              • 1168

              #7
              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.

              Comment

              • gsherp
                New Member
                • May 2007
                • 34

                #8
                Yeah...that is good. Thanks for all the help

                Comment

                • TheServant
                  Recognized Expert Top Contributor
                  • Feb 2008
                  • 1168

                  #9
                  No worries. Hope to see you around sometime.

                  Comment

                  Working...