bidimensional array from query

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Anna

    bidimensional array from query

    Hi, I have the following MySQL table:



    inner_id data1 data2 data3

    ->0 g sd ds

    1 a n y
    2 b o w

    ->3 c p aa
    ->3 d q bb
    ->3 e r cc

    4 h u ff

    5 i v gg
    5 l z hh

    6 m x i



    After a query, I need to put all data in a bidimensional array this way:
    every index of the array is an array, so that:

    my_array[0] is array("data1"=> g,"data2"=>sd," data3"=>ds)

    BUT

    my_array[3] is
    array(array("da ta1"=>c,"data2" =>p,"data3"=>aa ),array("data1" =>d,"data2"=>q, "data3"=>bb),ar ray("data1"=>e, "data2"=>r,"dat a3"=>cc))

    as for data1, data2, data3 labels, it is NOT important they match the
    name of the field ("data1", "data2" can be ok too).

    I simply can't do it: would you gently tell me how to do? Thanks!

  • Sanders Kaufman

    #2
    Re: bidimensional array from query

    Anna wrote:
    After a query, I need to put all data in a bidimensional array this way:
    every index of the array is an array, so that:
    >
    my_array[0] is array("data1"=> g,"data2"=>sd," data3"=>ds)
    I don't think you can make the INDEX (key) an array.
    The value - yes, but the key - no.

    What you've got there is an "associativ e" array.

    Comment

    • Anna

      #3
      Re: bidimensional array from query

      Sanders Kaufman ha scritto:
      Anna wrote:
      >
      >After a query, I need to put all data in a bidimensional array this way:
      >every index of the array is an array, so that:
      >>
      >my_array[0] is array("data1"=> g,"data2"=>sd," data3"=>ds)
      >
      I don't think you can make the INDEX (key) an array.
      The value - yes, but the key - no.
      >
      What you've got there is an "associativ e" array.


      maybe I did not explain well the problem:

      <?php
      mysql_connect(" localhost", "root", "") or
      die("Connession e non riuscita: " . mysql_error());
      mysql_select_db ("db");

      $result = mysql_query("SE LECT inner_id,data1, data2,data3 FROM test");

      $ext = array();


      while ($row = mysql_fetch_arr ay($result, MYSQL_NUM)) {

      // here I want to transform my result in a bidimensional array
      // where $row[0] is an index of an indexed numeric array of arrays
      // --->when inner_id is a unique value, it must be a single array,
      // --->while, if inner_id is multiple (that is, many records share that
      inner_id) the result must be pushed in an array of arrays

      // I'm sure it can be done but I don't know how

      $ext[$row[0]] = ...

      }


      ?>

      Comment

      • Anna

        #4
        Re: bidimensional array from query

        Hi, I was about to reach the solution but...


        By using this in that while loop:

        if(!$ext[$row[0]]){
        array_push($ext ,array("a"=>$ro w[1],"b"=>$row[2],"c"=>$row[3]));
        }
        else
        {
        array_push($ext[$row[0]],array("a"=>$ro w[1],"b"=>$row[2],"c"=>$row[3]));
        }


        I get:

        Array ( [0] =Array ( [a] =aa [b] =ab [c] =ac )
        [1] =Array ( [a] =ba [b] =bb [c] =bc )
        [2] =Array ( [a] =ca [b] =cb [c] =cc )
        [3] =Array ( [a] =da [b] =db [c] =dc
        [0] =Array ( [a] =ea [b] =eb [c] =ec )
        [1] =Array ( [a] =fa [b] =fb [c] =fc ) )
        [4] =Array ( [a] =ga [b] =gb [c] =gc )
        [5] =Array ( [a] =ha [b] =hb [c] =hc
        [0] =Array ( [a] =ia [b] =ib [c] =ic ) )
        [6] =Array ( [a] =la [b] =lb [c] =lc ) )


        This would be good, but there's a problem:

        the first element at index [3] must be [0] (then [1] and [2]) while at
        the moment it is "anonymous" , AND

        the first element at index [5] must be [0] too.


        Could anyone help me?




        Comment

        • Anna

          #5
          Re: bidimensional array from query

          THIS:


          Array ( [0] =Array ( [a] =aa [b] =ab [c] =ac )
          [1] =Array ( [a] =ba [b] =bb [c] =bc )
          [2] =Array ( [a] =ca [b] =cb [c] =cc )
          [3] =Array ( [a] =da [b] =db [c] =dc
          [0] =Array ( [a] =ea [b] =eb [c] =ec )
          [1] =Array ( [a] =fa [b] =fb [c] =fc ) )
          [4] =Array ( [a] =ga [b] =gb [c] =gc )
          [5] =Array ( [a] =ha [b] =hb [c] =hc
          [0] =Array ( [a] =ia [b] =ib [c] =ic ) )
          [6] =Array ( [a] =la [b] =lb [c] =lc ) )



          SHOULD BE:

          Array ( [0] =Array ( [a] =aa [b] =ab [c] =ac )
          [1] =Array ( [a] =ba [b] =bb [c] =bc )
          [2] =Array ( [a] =ca [b] =cb [c] =cc )
          [3] =Array ( [0] =Array([a] =da [b] =db [c] =dc)
          [1] =Array ( [a] =ea [b] =eb [c] =ec )
          [2] =Array ( [a] =fa [b] =fb [c] =fc ) )
          [4] =Array ( [a] =ga [b] =gb [c] =gc )
          [5] =Array ( [0] =Array([a] =ha [b] =hb [c] =hc)
          [1] =Array ( [a] =ia [b] =ib [c] =ic ) )
          [6] =Array ( [a] =la [b] =lb [c] =lc ) )

          Comment

          Working...