Associative Array with ID as Key and other associative array as value?

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

    Associative Array with ID as Key and other associative array as value?

    Hi,

    I want to create an associative Array with a PHP variable (article ID)
    as Key
    and another associative array as it's value.

    How do I instanciate it, how can I fill it?

    I want something like this:

    $articleTable= array( $articleNr => array("name" => $name, "price " =>
    $price ,"amount" => 0 );

    ( Amount will be filled later in a form.)

    At the end I want an Array that contains data coming from the
    database,
    something like this:

    ArticleNr ArticleName Price Amount
    12345 tooth bruth 10 3
    24939 stuff 2 1
    19492 tooth paste 8 0
    29491 water 1 4


    So how can this be done?

    Thanks,

    Stefan
  • Ken Robinson

    #2
    Re: Associative Array with ID as Key and other associative array as value?


    Stefan Richter wrote:[color=blue]
    > Hi,
    >
    > I want to create an associative Array with a PHP variable (article[/color]
    ID)[color=blue]
    > as Key
    > and another associative array as it's value.
    >
    > How do I instanciate it, how can I fill it?
    >
    > I want something like this:
    >
    > $articleTable= array( $articleNr => array("name" => $name, "price "[/color]
    =>[color=blue]
    > $price ,"amount" => 0 );[/color]

    You can do it like you show, except you need another closing ")". Or
    you can do it like this:

    $articleTable[$articleNr]['name'] = $name;
    $articleTable[$articleNr]['price'] = $price;
    $articleTable[$articleNr]['amount'] = 0;

    While you're debugging it, use
    echo '<pre>';print_r ($articleTable) ;echo '</pre>';
    to see the contents.

    Ken

    Comment

    • Erwin Moller

      #3
      Re: Associative Array with ID as Key and other associative array as value?

      Stefan Richter wrote:
      [color=blue]
      > Hi,
      >
      > I want to create an associative Array with a PHP variable (article ID)
      > as Key
      > and another associative array as it's value.
      >
      > How do I instanciate it, how can I fill it?
      >
      > I want something like this:
      >
      > $articleTable= array( $articleNr => array("name" => $name, "price " =>
      > $price ,"amount" => 0 );
      >
      > ( Amount will be filled later in a form.)
      >
      > At the end I want an Array that contains data coming from the
      > database,
      > something like this:
      >
      > ArticleNr ArticleName Price Amount
      > 12345 tooth bruth 10 3
      > 24939 stuff 2 1
      > 19492 tooth paste 8 0
      > 29491 water 1 4
      >
      >
      > So how can this be done?
      >
      > Thanks,
      >
      > Stefan[/color]


      Hi Stefan,

      That can be done in many ways in PHP.
      I often use similar construct to store some databasevalues in a ASSOC array,
      that needs to have some formatting so other functions can grok it.

      Here is an example (postgresql style, but you can probably adapt it easily)

      $myArticles = array();

      $SQL = "SELECT ArticleID, ArticleNr, ArticleName, Price, Amount FROM
      tblarticles";
      $RS = pg_exec($connec tion,$SQL);
      $numrows = pg_numrows($RS) ;
      for ($i=0;$i<$numro ws;$i++) {
      $row = pg_fetch_array( $RS,$i,PGSQL_AS SOC);
      $thisrow = array();
      $thisrow["ArticleID"] = $row["ArticleID"];
      $thisrow["ArticleNr"] = $row["ArticleNr"];
      $thisrow["ArticleNam e"] = $row["ArticleNam e"];
      $thisrow["Price"] = $row["Price"];
      $thisrow["Amount"] = $row["Amount"];

      // Now add to $myArticles
      $myArticles["ID".$thisr ow["ArticleID"]] = $thisrow;
      }

      // From here on $myArticles contains many arrays
      // stored under the name IDxxx where xxx is articleID


      Of course the above example is plain stupid, but it shows how to populate an
      array with arrays, using the ArticleID as key.

      Hope this helps you.
      Tip: Also have a look at foreach at php.net to easily loop over ASSOC arrays
      containing arrays, retrieving the key.

      foreach (array_expressi on as $key => $value) {
      // $key now contains ID3423
      // $value now is one of the arrays $thisrow
      }

      Good luck,

      Regards,
      Erwin Moller

      Comment

      • Matt Mitchell

        #4
        Re: Associative Array with ID as Key and other associative array as value?

        "Erwin Moller"
        <since_humans_r ead_this_I_am_s pammed_too_much @spamyourself.c om> wrote in
        message news:421cad39$0 $28975$e4fe514c @news.xs4all.nl ...
        : > So how can this be done?
        : >
        : > Thanks,
        : >
        : > Stefan
        :
        :
        : Hi Stefan,
        :
        : That can be done in many ways in PHP.
        : I often use similar construct to store some databasevalues in a ASSOC
        array,
        : that needs to have some formatting so other functions can grok it.
        :
        : Here is an example (postgresql style, but you can probably adapt it
        easily)
        :
        : $myArticles = array();
        :
        : $SQL = "SELECT ArticleID, ArticleNr, ArticleName, Price, Amount FROM
        : tblarticles";
        : $RS = pg_exec($connec tion,$SQL);
        : $numrows = pg_numrows($RS) ;
        : for ($i=0;$i<$numro ws;$i++) {
        : $row = pg_fetch_array( $RS,$i,PGSQL_AS SOC);
        : $thisrow = array();
        : $thisrow["ArticleID"] = $row["ArticleID"];
        : $thisrow["ArticleNr"] = $row["ArticleNr"];
        : $thisrow["ArticleNam e"] = $row["ArticleNam e"];
        : $thisrow["Price"] = $row["Price"];
        : $thisrow["Amount"] = $row["Amount"];
        :
        : // Now add to $myArticles
        : $myArticles["ID".$thisr ow["ArticleID"]] = $thisrow;
        : }

        Or even
        while ($row = pg_fetch_array( $RS, $i, PGSQL_ASSOC))
        {
        foreach ($row as $fld=>$val)
        {
        if ($fld == 'ArticleID') next;
        $myArticles[$row['ArticleID']][$fld] = $val;
        }
        }

        :
        : // From here on $myArticles contains many arrays
        : // stored under the name IDxxx where xxx is articleID
        :
        :
        : Of course the above example is plain stupid, but it shows how to populate
        an
        : array with arrays, using the ArticleID as key.

        Just a bit clunky!
        :
        : Hope this helps you.
        : Tip: Also have a look at foreach at php.net to easily loop over ASSOC
        arrays
        : containing arrays, retrieving the key.
        :
        : foreach (array_expressi on as $key => $value) {
        : // $key now contains ID3423
        : // $value now is one of the arrays $thisrow
        : }

        Why not do the same when populating the array?

        Matt


        Comment

        • Erwin Moller

          #5
          Re: Associative Array with ID as Key and other associative array as value?

          Matt Mitchell wrote:
          [color=blue]
          > "Erwin Moller"
          > <since_humans_r ead_this_I_am_s pammed_too_much @spamyourself.c om> wrote in
          > message news:421cad39$0 $28975$e4fe514c @news.xs4all.nl ...
          > : > So how can this be done?
          > : >
          > : > Thanks,
          > : >
          > : > Stefan
          > :
          > :
          > : Hi Stefan,
          > :
          > : That can be done in many ways in PHP.
          > : I often use similar construct to store some databasevalues in a ASSOC
          > array,
          > : that needs to have some formatting so other functions can grok it.
          > :
          > : Here is an example (postgresql style, but you can probably adapt it
          > easily)
          > :
          > : $myArticles = array();
          > :
          > : $SQL = "SELECT ArticleID, ArticleNr, ArticleName, Price, Amount FROM
          > : tblarticles";
          > : $RS = pg_exec($connec tion,$SQL);
          > : $numrows = pg_numrows($RS) ;
          > : for ($i=0;$i<$numro ws;$i++) {
          > : $row = pg_fetch_array( $RS,$i,PGSQL_AS SOC);
          > : $thisrow = array();
          > : $thisrow["ArticleID"] = $row["ArticleID"];
          > : $thisrow["ArticleNr"] = $row["ArticleNr"];
          > : $thisrow["ArticleNam e"] = $row["ArticleNam e"];
          > : $thisrow["Price"] = $row["Price"];
          > : $thisrow["Amount"] = $row["Amount"];
          > :
          > : // Now add to $myArticles
          > : $myArticles["ID".$thisr ow["ArticleID"]] = $thisrow;
          > : }
          >
          > Or even
          > while ($row = pg_fetch_array( $RS, $i, PGSQL_ASSOC))
          > {
          > foreach ($row as $fld=>$val)
          > {
          > if ($fld == 'ArticleID') next;
          > $myArticles[$row['ArticleID']][$fld] = $val;
          > }
          > }[/color]

          Yes yes, I know!
          I told you it was a stupid example.

          But in your example you make an exact copy of the row, while I was merely
          showing the idea.
          By doing it step by step you can also add other stuff to it.
          (That is how I use it)

          [color=blue]
          >
          > :
          > : // From here on $myArticles contains many arrays
          > : // stored under the name IDxxx where xxx is articleID
          > :
          > :
          > : Of course the above example is plain stupid, but it shows how to
          > : populate
          > an
          > : array with arrays, using the ArticleID as key.
          >
          > Just a bit clunky![/color]

          But clear I hope. :P
          [color=blue]
          > :
          > : Hope this helps you.
          > : Tip: Also have a look at foreach at php.net to easily loop over ASSOC
          > arrays
          > : containing arrays, retrieving the key.
          > :
          > : foreach (array_expressi on as $key => $value) {
          > : // $key now contains ID3423
          > : // $value now is one of the arrays $thisrow
          > : }
          >
          > Why not do the same when populating the array?[/color]

          Erm...
          We ARE populating arrays. :P
          [color=blue]
          >
          > Matt[/color]


          Regards,
          Erwin Moller

          Comment

          • Matt Mitchell

            #6
            Re: Associative Array with ID as Key and other associative array as value?

            "Erwin Moller"
            <since_humans_r ead_this_I_am_s pammed_too_much @spamyourself.c om> wrote in
            message news:421cb4c1$0 $28982$e4fe514c @news.xs4all.nl ...
            : > : for ($i=0;$i<$numro ws;$i++) {
            : > : $row = pg_fetch_array( $RS,$i,PGSQL_AS SOC);
            : > : $thisrow = array();
            : > : $thisrow["ArticleID"] = $row["ArticleID"];
            : > : $thisrow["ArticleNr"] = $row["ArticleNr"];
            : > : $thisrow["ArticleNam e"] = $row["ArticleNam e"];
            : > : $thisrow["Price"] = $row["Price"];
            : > : $thisrow["Amount"] = $row["Amount"];
            : > :
            : > : // Now add to $myArticles
            : > : $myArticles["ID".$thisr ow["ArticleID"]] = $thisrow;
            : > : }
            : >
            : > : foreach (array_expressi on as $key => $value) {
            : > : // $key now contains ID3423
            : > : // $value now is one of the arrays $thisrow
            : > : }
            : >
            : > Why not do the same when populating the array?
            :
            : Erm...
            : We ARE populating arrays. :P

            Erm, it was a reference to hard-coding the contents of the array, rather
            than looping through it...

            Matt "Erm" Mitchell


            Comment

            • Erwin Moller

              #7
              Re: Associative Array with ID as Key and other associative array as value?

              Matt Mitchell wrote:
              [color=blue]
              > "Erwin Moller"
              > : >
              > : > Why not do the same when populating the array?
              > :
              > : Erm...
              > : We ARE populating arrays. :P
              >
              > Erm, it was a reference to hard-coding the contents of the array, rather
              > than looping through it...[/color]

              Ok.
              I think we confusing the original poster now. :-)
              [color=blue]
              >
              > Matt "Erm" Mitchell[/color]

              :-)

              Regards,
              Erwin "Erm too" Moller

              Comment

              • JDS

                #8
                Re: Associative Array with ID as Key and other associative array as value?

                On Wed, 23 Feb 2005 08:00:42 -0800, Stefan Richter wrote:
                [color=blue]
                > I want something like this:
                >
                > $articleTable= array( $articleNr => array("name" => $name, "price " =>
                > $price ,"amount" => 0 );[/color]

                Well that should work just fine (except for a syntax error). What is the
                trouble?

                --
                JDS | jeffrey@example .invalid
                | http://www.newtnotes.com
                DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

                Comment

                • JDS

                  #9
                  Re: Associative Array with ID as Key and other associative array as value?

                  On Wed, 23 Feb 2005 18:00:55 +0100, Erwin Moller wrote:
                  [color=blue]
                  > Ok.
                  > I think we confusing the original poster now. :-)[/color]

                  <ha!>
                  You forgot to mention bi-spline combinatorial population of arrays as a
                  possible solution. By taking the octal root of the spline, one can
                  generate multi-layered, multi-dimensional sub-arrays that conform to
                  international spec IEEAC-091499, are manageable within tri-polar dihedral
                  clusters, can autonomously recombine into sub-sub-arrays, and also look
                  real nice.
                  </ha!>

                  Confused now?
                  --
                  JDS | jeffrey@example .invalid
                  | http://www.newtnotes.com
                  DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

                  Comment

                  • Erwin Moller

                    #10
                    Re: Associative Array with ID as Key and other associative array as value?

                    JDS wrote:
                    [color=blue]
                    > On Wed, 23 Feb 2005 18:00:55 +0100, Erwin Moller wrote:
                    >[color=green]
                    >> Ok.
                    >> I think we confusing the original poster now. :-)[/color]
                    >
                    > <ha!>
                    > You forgot to mention bi-spline combinatorial population of arrays as a
                    > possible solution. By taking the octal root of the spline, one can
                    > generate multi-layered, multi-dimensional sub-arrays that conform to
                    > international spec IEEAC-091499, are manageable within tri-polar dihedral
                    > clusters, can autonomously recombine into sub-sub-arrays, and also look
                    > real nice.
                    > </ha!>
                    >
                    > Confused now?[/color]

                    Wow!
                    Yes, I am confused now.

                    I have totally 100% NO-CLUE what you are talking about.
                    :P

                    But I must admit: It sounds REALLY SOPHISTICATED!

                    Maybe you can get a patent on that and sell it to M$?
                    (Please give me 1% idiotfee)

                    Thanks. :P

                    Regards,
                    Erwin Moller

                    Comment

                    • Matt Mitchell

                      #11
                      Re: Associative Array with ID as Key and other associative array as value?

                      "JDS" <jeffrey@exampl e.invalid> wrote in message
                      news:pan.2005.0 2.23.17.35.09.1 42283@example.i nvalid...
                      : > Ok.
                      : > I think we confusing the original poster now. :-)
                      :
                      : <ha!>
                      : You forgot to mention bi-spline combinatorial population of arrays as a
                      : possible solution. By taking the octal root of the spline, one can
                      : generate multi-layered, multi-dimensional sub-arrays that conform to
                      : international spec IEEAC-091499, are manageable within tri-polar dihedral
                      : clusters, can autonomously recombine into sub-sub-arrays, and also look
                      : real nice.
                      : </ha!>

                      Ah, but these are much more effective when coded in Intercal... And don't
                      forget, PHP *still* hasn't implemented the "COME FROM" statement.

                      :
                      : Confused now?

                      LMFAO!

                      Seriously though, it's just that so many people posting in here seem to be
                      unaware of constructs like foreach http://www.php.net/foreach and the perl
                      regex stuff http://www.php.net/pcre , that contain much of the power of PHP
                      to make clean, easily maintainable, elegant code.

                      These were some of the things I really liked about Perl and PHP when I
                      started. Otherwise, it really *would* make more sense to code things in
                      VBScript...

                      Matt


                      Comment

                      • JDS

                        #12
                        Re: Associative Array with ID as Key and other associative array as value?

                        On Wed, 23 Feb 2005 18:46:54 +0000, Matt Mitchell wrote:
                        [color=blue]
                        > Seriously though, it's just that so many people posting in here seem to be
                        > unaware of constructs like foreach http://www.php.net/foreach and the perl[/color]

                        Yes. Seriously. How often do we have to see "How do I print all the
                        elements in an array?" Read a friggin book, hoss!

                        Or please please please Google:
                        <http://www.google.com/search?hl=en&lr =&safe=off&clie nt=firefox-a&rls=org.mozil la%3Aen-US%3Aofficial&q =php+How+do+I+p rint+all+the+el ements+in+an+ar ray%3F&btnG=Sea rch>

                        wheeeeeeeeeeeee eeeee

                        --
                        JDS | jeffrey@example .invalid
                        | http://www.newtnotes.com
                        DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

                        Comment

                        Working...