How to insert an array into an array?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mfaisalwarraich
    New Member
    • Oct 2007
    • 194

    How to insert an array into an array?

    Hi everyone,

    I am not very much familiar while working with arrays. I have a situation where i have to add an array into an array. the code without loop is:
    Code:
    $value =  array(array('Ottawa, Canada', 'Ottawa'), array('Montreal, Canada', 'Montreal'));
    Now im trying to use it with while loop something like this

    Code:
    while($rs = mysql_fetch_assoc($qry))
    {
    	$add_addresses[] = array($rs['address'], $rs['country']);
    }
    This is failing me because its showing a single array whereas i need arrays inside an array. Please guide me how i can achieve this.

    thank you.
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    You mean a 2d array? A PHP 2d array is actually an array inside an array, and not a 2d matrix as in C, so this can be done
    Although I am struggling to understand what you are trying to do.
    This is failing me because its showing a single array
    A concise desrciption of your aim is more helpful than a vague 'This is failing me..'

    Comment

    • mfaisalwarraich
      New Member
      • Oct 2007
      • 194

      #3
      thank you for ur reply. wat im trying to do is saving each row of a table into separate array in such way that each array holds a separate value of address inside a while loop.

      like i have 2 records inside a mysql table. now im trying to fetch its records using while loop

      Code:
      while($rs = mysql_fetch_assoc($qry))
      {
          echo $value = $rs['address'],'-',$rs['country'];
      }
      The above working fine. Now i want to make each row of $rs as an array for example if i have 2 records there will be 2 $value arrays holding different values.

      All this is required cuz im trying to make google map. Here is the code
      Code:
      for ($i=0; $i<count($points); $i++) {
        $api->addGeoPoint($points[$i][0],$points[$i][1], $points[$i][2] , true);
      }
      $addresses = array(array('Montreal, Canada', 'Canada'), array('Ottawa, Canada', 'Canada'),array('Sydney, Australia', 'Australia'),array('Norway, Norway', 'Norway'));
      
      // add the addresses
      for ($i=0; $i<count($addresses); $i++) {
        $api->addAddress($addresses[$i][0], $addresses[$i][1], true);
      }
      Notice $addresses array which is here hard coded with different arrays holding address values. But im trying to get that values from a database table by way of making each record as a separate array. i have downloaded a class from phpclasses which im using here. i hope now u will get idea wat im trying to do here and will guide me accordingly.

      thank u.

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        $rs = mysql_fetch_ass oc($qry) actually returns an array with the field names as keys.
        If you want all the rows saved into a 2d array then
        Code:
        while($rs = mysql_fetch_assoc($qry)) 
        { 
            $add_addresses[] = $rs; 
        }
        I can't see where the result is being used in the google map code

        Comment

        • mfaisalwarraich
          New Member
          • Oct 2007
          • 194

          #5
          as i mentioned there is a class name nxgooglemapsapi .php available at phpclasses dot org. im trying to work with this class. here is the complete call code
          Code:
          $api = new NXGoogleMapsAPI();
          
          // setup the visual design of the control
          $api->setWidth(465);
          $api->setHeight(312);
          $api->setZoomFactor(1);
          $api->addControl(GSmallMapControl);
          
          // define points
          //$points = array(array(50,10,'Point1'), array(51,15, 'Point2'));
          
          // add a point. 
          for ($i=0; $i<count($points); $i++) {
            $api->addGeoPoint($points[$i][0],$points[$i][1], $points[$i][2] , true);
          }
          $qry = mysql_query("select address,country from tbl");
          
          while($rs = mysql_fetch_assoc($qry))
          {
          	$add_addresses[] = $rs;
          }
          //this not working
          $addresses = $add_addresses;
          
          //this is working
          //$addresses = array(array('Ottawa, Canada', 'Canada'),array('Sydney, Australia', 'Australia'),array('Norway, Norway', 'Norway'));
          
          // add the addresses
          for ($i=0; $i<count($addresses); $i++) {
            $api->addAddress($addresses[$i][0], $addresses[$i][1], true);
          }
          
           echo $api->getHeadCode(); 
          
          echo $api->getBodyCode();
          ?>
          I tried wat u suggested but its still not working.

          Comment

          • code green
            Recognized Expert Top Contributor
            • Mar 2007
            • 1726

            #6
            You are still replying with vague 'this not working', instead of better explaining your aim.
            Are you saying that you want the result from the query
            Code:
            while($rs = mysql_fetch_assoc($qry)) 
            { 
                $add_addresses[] = $rs; 
            }
            Manipulating into the format
            Code:
            array(array('Ottawa, Canada', 'Canada'),
            array('Sydney, Australia', 'Australia'),
            array('Norway, Norway', 'Norway')....)

            Comment

            • mfaisalwarraich
              New Member
              • Oct 2007
              • 194

              #7
              yes i want to manipulate it result of query into the following format:

              Code:
              array(array('Ottawa, Canada', 'Canada'),
              array('Sydney, Australia', 'Australia'),
              array('Norway, Norway', 'Norway')....)

              Comment

              • code green
                Recognized Expert Top Contributor
                • Mar 2007
                • 1726

                #8
                OK. And what do the query results look like?
                That is the contents of each $rs from
                Code:
                while($rs = mysql_fetch_assoc($qry))
                If you have commas in the address field that will cause problems now and in the future.

                Comment

                • mfaisalwarraich
                  New Member
                  • Oct 2007
                  • 194

                  #9
                  please test the code i provided with the class im working with. it will give u better idea wat is exactly going on. i tried $rs contents but its failing me.

                  thank you.

                  Comment

                  • code green
                    Recognized Expert Top Contributor
                    • Mar 2007
                    • 1726

                    #10
                    please test the code i provided with the class im working with
                    No thanks. What does the data returned from your query look like?
                    I can't advise on how to manipulate into the required format unless I know what it looks like originally.

                    Comment

                    • mfaisalwarraich
                      New Member
                      • Oct 2007
                      • 194

                      #11
                      Code:
                      while($rs = mysql_fetch_assoc($qry))
                      {
                      	$add_addresses[] = array(array($rs['address'], $rs['country']));
                      }
                      print_r($add_addresses);
                      result:
                      Code:
                      Array ( [0] => Array ( [0] => Array ( [0] => Ottawa,Canada [1] => Canada ) ) [1] => Array ( [0] => Array ( [0] => Sydney, Australia [1] => Australia ) ) )

                      Comment

                      • mfaisalwarraich
                        New Member
                        • Oct 2007
                        • 194

                        #12
                        okie i got it :) removed this:

                        Code:
                         $add_addresses[] = array(array($rs['address'], $rs['country']));
                        with this one
                        Code:
                         $add_addresses[] = array($rs['address'], $rs['country']);
                        and it worked. thank you for your help CG.

                        Comment

                        Working...