quick question

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

    quick question

    Hi,

    I've got a multidimensiona l array and I can loop through it fine but I can't
    get the values of first parts of the array - ie:

    foreach ($a as $v1) {
    foreach ($v1 as $v2) {
    echo "$v2\n";
    }
    }

    I can print $v2 straight out, but if I try to echo $v1 I get 'Array', I
    realise that it is an array nested within the original array but it still
    has a value which I need to determine.

    Thanks
    Alex



  • Alvaro G Vicario

    #2
    Re: quick question

    *** Alex Hopson wrote/escribió (Mon, 21 Jun 2004 17:18:49 +0100):[color=blue]
    > I can print $v2 straight out, but if I try to echo $v1 I get 'Array'[/color]

    What would you need to print instead?


    --
    --
    -- Álvaro G. Vicario - Burgos, Spain
    --

    Comment

    • oeb -=- bleh bleh bleh

      #3
      Re: quick question


      "Alex Hopson" <alex*under_sco re*hopson@hotma il.com> wrote in message
      news:vNDBc.2461 $q7.2311@fe09.u senetserver.com ...[color=blue]
      > Hi,
      >
      > I've got a multidimensiona l array and I can loop through it fine but I[/color]
      can't[color=blue]
      > get the values of first parts of the array - ie:
      >
      > foreach ($a as $v1) {
      > foreach ($v1 as $v2) {
      > echo "$v2\n";
      > }
      > }
      >
      > I can print $v2 straight out, but if I try to echo $v1 I get 'Array', I
      > realise that it is an array nested within the original array but it still
      > has a value which I need to determine.
      >
      > Thanks
      > Alex[/color]


      That's because $v1 is an Array. If you want to see all the contents of it
      try print_r($v1);


      Comment

      • Jeffrey Silverman

        #4
        Re: quick question

        On Mon, 21 Jun 2004 17:18:49 +0100, Alex Hopson wrote:
        [color=blue]
        > Hi,
        >
        > I've got a multidimensiona l array and I can loop through it fine but I can't
        > get the values of first parts of the array - ie:
        >
        > foreach ($a as $v1) {
        > foreach ($v1 as $v2) {
        > echo "$v2\n";
        > }
        > }
        >
        > I can print $v2 straight out, but if I try to echo $v1 I get 'Array', I
        > realise that it is an array nested within the original array but it still
        > has a value which I need to determine.
        >
        > Thanks
        > Alex[/color]

        You can't echo out an entire array. You just said it yourself that you
        realize that $v1 is an array.

        You can only use the indexed elements.

        You are doing it correctly, above, if you want to iterate through all
        items in the second-level array. But if you want to use a specific
        element of the second-level array, use their index. For example:

        <?php
        $a = array(
        array( "red", "green", "blue")
        ,array( "apple", "banana", "cherry")
        );
        ?>

        Using your above code, to get the second element in the second-dimension
        arrays, you would use $v1[1], e.g.:

        <?php
        foreach ($a as $v1) {
        echo $v1[1] . "<br>\n";
        }
        ?>

        That would echo:

        green<br>
        banana<br>

        Got it? Did I answer your question? I hope so.

        Later...

        --
        Jeffrey D. Silverman | jeffrey AT jhu DOT edu
        Website | http://www.wse.jhu.edu/newtnotes/

        Comment

        • Alex Hopson

          #5
          Re: quick question

          Hi,

          That's not exactly what I'm looking for (probably shouldn't have been so
          vague :( ). Here' my array that I've got:

          array(2) { [19]=> array(1) { ["blue"]=> array(1) { [0]=> int(2) } } [20]=>
          array(1) { ["green"]=> array(1) { [0]=> int(1) } } }

          and the code:

          $items=0
          if(is_array($ca rt)) { //check cart is an array
          foreach ($cart as $productid) { //loop through products
          foreach ($productid as $colour) { //loop through colours
          foreach ($colour as $option =>$qty) { //loop through options
          $items += $qty; //add qtry to total
          }
          }
          }
          }

          I'm using an indexed array (fr example the first product id is 19 and the
          first (and only) colour for that id is 'blue' which then refrences to
          another array with the options in.

          What I'm after is the name of the index, ie the '19' and the 'blue'

          I hope that's a little clearer :)

          Thanks
          Alex


          "Jeffrey Silverman" <jeffrey@jhu.ed u> wrote in message
          news:pan.2004.0 6.21.16.28.57.5 48603@jhu.edu.. .[color=blue]
          > On Mon, 21 Jun 2004 17:18:49 +0100, Alex Hopson wrote:
          >[color=green]
          > > Hi,
          > >
          > > I've got a multidimensiona l array and I can loop through it fine but I[/color][/color]
          can't[color=blue][color=green]
          > > get the values of first parts of the array - ie:
          > >
          > > foreach ($a as $v1) {
          > > foreach ($v1 as $v2) {
          > > echo "$v2\n";
          > > }
          > > }
          > >
          > > I can print $v2 straight out, but if I try to echo $v1 I get 'Array', I
          > > realise that it is an array nested within the original array but it[/color][/color]
          still[color=blue][color=green]
          > > has a value which I need to determine.
          > >
          > > Thanks
          > > Alex[/color]
          >
          > You can't echo out an entire array. You just said it yourself that you
          > realize that $v1 is an array.
          >
          > You can only use the indexed elements.
          >
          > You are doing it correctly, above, if you want to iterate through all
          > items in the second-level array. But if you want to use a specific
          > element of the second-level array, use their index. For example:
          >
          > <?php
          > $a = array(
          > array( "red", "green", "blue")
          > ,array( "apple", "banana", "cherry")
          > );
          > ?>
          >
          > Using your above code, to get the second element in the second-dimension
          > arrays, you would use $v1[1], e.g.:
          >
          > <?php
          > foreach ($a as $v1) {
          > echo $v1[1] . "<br>\n";
          > }
          > ?>
          >
          > That would echo:
          >
          > green<br>
          > banana<br>
          >
          > Got it? Did I answer your question? I hope so.
          >
          > Later...
          >
          > --
          > Jeffrey D. Silverman | jeffrey AT jhu DOT edu
          > Website | http://www.wse.jhu.edu/newtnotes/
          >[/color]



          Comment

          • Jeffrey Silverman

            #6
            Re: quick question

            On Mon, 21 Jun 2004 18:02:00 +0100, Alex Hopson wrote:
            [color=blue]
            > I'm using an indexed array (fr example the first product id is 19 and the
            > first (and only) colour for that id is 'blue' which then refrences to
            > another array with the options in.
            >
            > What I'm after is the name of the index, ie the '19' and the 'blue'
            >
            > I hope that's a little clearer :)
            >
            > Thanks
            > Alex[/color]

            I'm still not completely clear, but the code in your post is not formatted
            well in my newsreader. Also, it doesn't appear to be correct PHP code.
            Try posting the exact code from your script, not from print_r().

            Anyways, I think you need to use the "foreach ($array as $key=>$value)"
            syntax:

            <?php
            $a = array("19"=>"bl ue", "20"=>"red" );

            foreach ($a as $k => $v){
            echo "KEY is $k\n";
            echo "VAL is $v\n";
            }
            ?>

            This would set $k to 19 and $v to "blue" for the first item and "20" and
            "red" for the second.
            --
            Jeffrey D. Silverman | jeffrey AT jhu DOT edu
            Website | http://www.wse.jhu.edu/newtnotes/

            Comment

            • Alex Hopson

              #7
              Re: quick question

              Thanks. That's got it sorted now :D, I had tried that before but had the key
              and the val round the wrong way.

              Alex

              "Jeffrey Silverman" <jeffrey@jhu.ed u> wrote in message
              news:pan.2004.0 6.21.17.08.50.5 71985@jhu.edu.. .[color=blue]
              > On Mon, 21 Jun 2004 18:02:00 +0100, Alex Hopson wrote:
              >[color=green]
              > > I'm using an indexed array (fr example the first product id is 19 and[/color][/color]
              the[color=blue][color=green]
              > > first (and only) colour for that id is 'blue' which then refrences to
              > > another array with the options in.
              > >
              > > What I'm after is the name of the index, ie the '19' and the 'blue'
              > >
              > > I hope that's a little clearer :)
              > >
              > > Thanks
              > > Alex[/color]
              >
              > I'm still not completely clear, but the code in your post is not formatted
              > well in my newsreader. Also, it doesn't appear to be correct PHP code.
              > Try posting the exact code from your script, not from print_r().
              >
              > Anyways, I think you need to use the "foreach ($array as $key=>$value)"
              > syntax:
              >
              > <?php
              > $a = array("19"=>"bl ue", "20"=>"red" );
              >
              > foreach ($a as $k => $v){
              > echo "KEY is $k\n";
              > echo "VAL is $v\n";
              > }
              > ?>
              >
              > This would set $k to 19 and $v to "blue" for the first item and "20" and
              > "red" for the second.
              > --
              > Jeffrey D. Silverman | jeffrey AT jhu DOT edu
              > Website | http://www.wse.jhu.edu/newtnotes/
              >[/color]



              Comment

              • Margaret MacDonald

                #8
                Re: quick question

                Alex Hopson wrote:
                [color=blue]
                >Hi,
                >
                >I've got a multidimensiona l array and I can loop through it fine but I can't
                >get the values of first parts of the array - ie:
                >
                >foreach ($a as $v1) {
                > foreach ($v1 as $v2) {
                > echo "$v2\n";
                > }
                >}
                >
                >I can print $v2 straight out, but if I try to echo $v1 I get 'Array', I
                >realise that it is an array nested within the original array but it still
                >has a value which I need to determine.
                >
                >Thanks
                >Alex
                >
                >[/color]

                Write a small function to recursively descend through the array, eg:

                function show_array ( $s, $a )
                {
                echo '<table cellspacing="2" cellpadding="2 border="1" >
                <tr><td>======& nbsp;' . $s . '&nbsp;====== </td></tr>' ;
                if ( ! is_array($a) )
                { echo '<br>Cannot show: not an array!<br>' ; return ; }
                foreach ( $a as $k => $v )
                {
                echo '<tr>' ;
                if ( is_array( $v ) )
                {
                show_array( $k, $v ) ;
                }
                else
                echo '<td>' . $k . '</td><td>' . $v . '</td>' ;
                echo '</tr>' ;
                }
                }


                HTH,
                Margaret
                --
                (To mail me, please change .not.invalid to .net, first.
                Apologies for the inconvenience.)

                Comment

                Working...