How to print a multidimensional associative array?

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

    How to print a multidimensional associative array?

    I have the following associative array:

    $user["john"]["mozilla"]=1;
    $user["john"]["xmms"]=1;
    $user["doe"]["mozilla"]=0;
    $user["doe"]["office"]=1;
    $user["paul"]["mozilla"]=0;
    $user["paul"]["xmms"]=1;
    $user["paul"]["office"]=1;

    How can I print such an associative array using a loop statement like foreach??
  • JDS

    #2
    Re: How to print a multidimensiona l associative array?

    On Tue, 15 Feb 2005 09:40:25 -0800, Leonardo wrote:
    [color=blue]
    > How can I print such an associative array using a loop statement like foreach??[/color]

    Hmmm...

    Look it up



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

    Comment

    • NC

      #3
      Re: How to print a multidimensiona l associative array?

      Leonardo wrote:[color=blue]
      >
      > I have the following associative array:
      >
      > $user["john"]["mozilla"]=1;
      > $user["john"]["xmms"]=1;
      > $user["doe"]["mozilla"]=0;
      > $user["doe"]["office"]=1;
      > $user["paul"]["mozilla"]=0;
      > $user["paul"]["xmms"]=1;
      > $user["paul"]["office"]=1;
      >
      > How can I print such an associative array using a loop statement
      > like foreach??[/color]

      By nesting foreach() statements:

      foreach ($user as $name) {
      if (is_array($name )) {
      foreach ($name as $application) {
      echo $application;
      }
      } else {
      echo $name;
      }
      }

      However, if you only need to print it for debugging purposes,
      print_r($user) would suite you just fine. Also, you might
      want to take a look at var_dump() and var_export():

      Prints human-readable information about a variable


      Outputs or returns a parsable string representation of a variable


      Cheers,
      NC

      Comment

      • rush

        #4
        Re: How to print a multidimensiona l associative array?

        "Leonardo" <leonardomachad o@gmail.com> wrote in message
        news:a3c20c30.0 502150940.5947a 35@posting.goog le.com...[color=blue]
        > I have the following associative array:
        >
        > How can I print such an associative array using a loop statement like[/color]
        foreach??

        you may as well try print_r like in

        echo "<pre>";
        print_r($user);
        echo "</pre>";

        rush
        --
        Get your very own domain easily. Fast and professional customer service.




        Comment

        • Leonardo

          #5
          Re: How to print a multidimensiona l associative array?

          Thaks everybody for all thoses suggestions.

          I said that I need to print. But actually I need to process every data
          on this associative array. That's why print_r doesn't work for me.

          So, here is the associative array again,

          $user["john"]["mozilla"]=1;
          $user["john"]["xmms"]=1;
          $user["doe"]["mozilla"]=0;
          $user["doe"]["office"]=1;
          $user["paul"]["mozilla"]=0;
          $user["paul"]["xmms"]=1;
          $user["paul"]["office"]=1;

          I need the following output in my foreach statement:

          john mozilla 1
          john xmms 1
          doe mozilla 0
          doe office 1
          etc...

          If I got the above output from my associative array, I will be able to
          genarate the statistics I need, processing each element (instead of
          just printing them).

          Comment

          • Anonymous

            #6
            Re: How to print a multidimensiona l associative array?

            "Leonardo" <leonardomachad o@gmail.com> wrote in message
            news:a3c20c30.0 502151537.5d2e1 9b@posting.goog le.com...[color=blue]
            > Thaks everybody for all thoses suggestions.
            >
            > I said that I need to print. But actually I need to process every data
            > on this associative array. That's why print_r doesn't work for me.
            >
            > So, here is the associative array again,
            >
            > $user["john"]["mozilla"]=1;
            > $user["john"]["xmms"]=1;
            > $user["doe"]["mozilla"]=0;
            > $user["doe"]["office"]=1;
            > $user["paul"]["mozilla"]=0;
            > $user["paul"]["xmms"]=1;
            > $user["paul"]["office"]=1;
            >
            > I need the following output in my foreach statement:
            >
            > john mozilla 1
            > john xmms 1
            > doe mozilla 0
            > doe office 1
            > etc...
            >
            > If I got the above output from my associative array, I will be able to
            > genarate the statistics I need, processing each element (instead of
            > just printing them).[/color]

            To expand on NC's example in your format:

            foreach ($user as $name => $apps) {
            if(is_array($ap ps)) {
            foreach ($apps as $app => $val) {
            echo "$name $app $val";
            }
            } else {
            echo $name;
            }
            }

            --
            Anonymous

            "Treat your password like your toothbrush.
            Don't let anybody else use it, and get
            a new one every six months."
            ---Clifford Stoll


            Comment

            • Matt Mitchell

              #7
              Re: How to print a multidimensiona l associative array?


              "Leonardo" <leonardomachad o@gmail.com> wrote in message
              news:a3c20c30.0 502151537.5d2e1 9b@posting.goog le.com...[color=blue]
              > Thaks everybody for all thoses suggestions.
              > $user["doe"]["office"]=1;
              > $user["paul"]["mozilla"]=0;
              > $user["paul"]["xmms"]=1;
              > $user["paul"]["office"]=1;
              >
              > I need the following output in my foreach statement:
              >
              > john mozilla 1
              > john xmms 1
              > doe mozilla 0
              > doe office 1
              > etc...
              >
              > If I got the above output from my associative array, I will be able to
              > genarate the statistics I need, processing each element (instead of
              > just printing them).[/color]

              OK, I give up - here's the fish.

              foreach ($user as $fname=>$stats)
              {
              foreach ($stats as $item=>$value)
              {
              print "$user -> $item = $value\n";
              }
              }

              Maybe if you spent a minute or two reading what's there on
              http://php.net/foreach ,
              and trying some code out, you could have solved the problem using far less
              effort
              than it has taken telling everybody how you don't want to read and learn the
              answer
              yourself.

              Matt


              Comment

              • Geoff Berrow

                #8
                Re: How to print a multidimensiona l associative array?

                I noticed that Message-ID:
                <QGwQd.98976$B8 .69881@fe3.news .blueyonder.co. uk> from Matt Mitchell
                contained the following:
                [color=blue]
                >OK, I give up - here's the fish.[/color]

                LOL...

                --
                Geoff Berrow (put thecat out to email)
                It's only Usenet, no one dies.
                My opinions, not the committee's, mine.
                Simple RFDs http://www.ckdog.co.uk/rfdmaker/

                Comment

                • Sacs

                  #9
                  Re: How to print a multidimensiona l associative array?

                  Leonardo wrote:[color=blue]
                  > Thaks everybody for all thoses suggestions.
                  >
                  > I said that I need to print. But actually I need to process every data
                  > on this associative array. That's why print_r doesn't work for me.
                  >
                  > So, here is the associative array again,
                  >
                  > $user["john"]["mozilla"]=1;
                  > $user["john"]["xmms"]=1;
                  > $user["doe"]["mozilla"]=0;
                  > $user["doe"]["office"]=1;
                  > $user["paul"]["mozilla"]=0;
                  > $user["paul"]["xmms"]=1;
                  > $user["paul"]["office"]=1;
                  >
                  > I need the following output in my foreach statement:
                  >
                  > john mozilla 1
                  > john xmms 1
                  > doe mozilla 0
                  > doe office 1
                  > etc...
                  >
                  > If I got the above output from my associative array, I will be able to
                  > genarate the statistics I need, processing each element (instead of
                  > just printing them).[/color]

                  A little ugly, but:

                  <?php
                  $user["john"]["mozilla"]=1;
                  $user["john"]["xmms"]=1;
                  $user["doe"]["mozilla"]=0;
                  $user["doe"]["office"]=1;
                  $user["paul"]["mozilla"]=0;
                  $user["paul"]["xmms"]=1;
                  $user["paul"]["office"]=1;

                  $keys = array_keys($use r);
                  for($i = 0; $i < count($keys); $i++) {
                  $user_keys = array_keys($use r[$keys[$i]]);
                  for($j = 0; $j < count($user_key s); $j++) {
                  print($keys[$i] . ' ' . $user_keys[$j] . ' ' .
                  $user[$keys[$i]][$user_keys[$j]]
                  .. "\n");
                  }
                  }
                  ?>

                  ?
                  Sacs

                  ~
                  ~

                  Comment

                  • Leonardo

                    #10
                    Re: How to print a multidimensiona l associative array?

                    > A little ugly, but:

                    .... it works! Thank you.

                    Comment

                    • JDS

                      #11
                      Re: How to print a multidimensiona l associative array?

                      On Wed, 16 Feb 2005 03:03:42 -0800, Leonardo wrote:
                      [color=blue][color=green]
                      >> A little ugly, but:[/color]
                      >
                      > ... it works! Thank you.[/color]

                      But ugly is bad in the long run. Works now, yes... but how about later,
                      when you want to change, add, debug, or modify the thing?

                      Matt Mitchell's example is way better.

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

                      Comment

                      • JDS

                        #12
                        Re: How to print a multidimensiona l associative array?

                        On Wed, 16 Feb 2005 00:43:28 +0000, Matt Mitchell wrote:
                        [color=blue]
                        > OK, I give up - here's the fish.[/color]

                        Awww... you gave up too easily.

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

                        Comment

                        • Matt Mitchell

                          #13
                          Give a man a fish...


                          "JDS" <jeffrey@exampl e.invalid> wrote in message
                          news:pan.2005.0 2.16.14.50.29.3 22406@example.i nvalid...[color=blue]
                          > On Wed, 16 Feb 2005 00:43:28 +0000, Matt Mitchell wrote:
                          >[color=green]
                          >> OK, I give up - here's the fish.[/color]
                          >
                          > Awww... you gave up too easily.
                          >[/color]

                          "Give a man a fish, and you feedcx him for a day; teach a man how to fish,
                          and you feed him for life."

                          I heard a much much better version the other day:

                          "Give a man a hot meal, and you'll keep him warm for an hour. Set a man on
                          fire, and you'll keep him warm for the rest of his life."

                          Somehow it sprang to mind when I was reading this thread...

                          Matt


                          Comment

                          • JDS

                            #14
                            Re: Give a man a fish...

                            On Wed, 16 Feb 2005 16:37:58 +0000, Matt Mitchell wrote:
                            [color=blue]
                            > "Give a man a fish, and you feedcx him for a day; teach a man how to fish,
                            > and you feed him for life."[/color]

                            Actually, I got it. Thanks.

                            <http://groups-beta.google.com/group/alt.php/browse_frm/thread/b199404da0add7b b/fccd3327a22daa0 6?q=jeffrey@jhu .edu+OR+jeffrey pants@jhu.edu+O R+jeffrey@go.aw ay.com+OR+jeffr ey@pantsjhu.edu &_done=%2Fgroup s%3Fq%3Djeffrey @jhu.edu+OR+jef freypants@jhu.e du+OR+jeffrey@g o.away.com+OR+j effrey@pantsjhu .edu%26start%3D 0%26scoring%3Dd %26hl%3Den%26ie %3DUTF-8%26oe%3DUTF-8%26safe%3Doff% 26&_doneTitle=B ack+to+Search&& d#fccd3327a22da a06>

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

                            Comment

                            • Matt Mitchell

                              #15
                              Re: Give a man a fish...


                              "JDS" <jeffrey@exampl e.invalid> wrote in message
                              news:pan.2005.0 2.16.16.45.43.7 72940@example.i nvalid...[color=blue]
                              > On Wed, 16 Feb 2005 16:37:58 +0000, Matt Mitchell wrote:
                              >[color=green]
                              >> "Give a man a fish, and you feedcx him for a day; teach a man how to
                              >> fish,
                              >> and you feed him for life."[/color]
                              >
                              > Actually, I got it. Thanks.
                              >[/color]

                              I know you got it - just thought you might find the alternative version
                              amusing!...

                              <g>

                              Matt


                              Comment

                              Working...