Multidimensional associated array

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

    #1

    Multidimensional associated array

    Hi,

    I try to construct an array such that each element of it is another
    array returned by the fetch_array_fun ction:

    for ( $i = 1; $i<=$n; $i++ )
    {
    $ar[$i] = mysql_fetch_arr ay( $result );
    print "$ar[$i][fieldname]\n";
    }

    It prints "Array[fieldname]".

    If I replace $ar[$i] by $ar everything works fine! Could you pleas
    help me with that?
  • Jerry Stuckle

    #2
    Re: Multidimensiona l associated array

    Kurda Yon wrote:
    Hi,
    >
    I try to construct an array such that each element of it is another
    array returned by the fetch_array_fun ction:
    >
    for ( $i = 1; $i<=$n; $i++ )
    {
    $ar[$i] = mysql_fetch_arr ay( $result );
    print "$ar[$i][fieldname]\n";
    }
    >
    It prints "Array[fieldname]".
    >
    If I replace $ar[$i] by $ar everything works fine! Could you pleas
    help me with that?
    >
    print $ar[$i]['fieldname'] . "\n";
    or
    print "{$ar[$i]['fieldname']\n";

    [fieldname] will give you an E_NOTICE if enabled (and it should be on
    your development system).

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • Norman Peelman

      #3
      Re: Multidimensiona l associated array

      Jerry Stuckle wrote:
      Kurda Yon wrote:
      >Hi,
      >>
      >I try to construct an array such that each element of it is another
      >array returned by the fetch_array_fun ction:
      >>
      >for ( $i = 1; $i<=$n; $i++ )
      >{
      >$ar[$i] = mysql_fetch_arr ay( $result );
      >print "$ar[$i][fieldname]\n";
      >}
      >>
      >It prints "Array[fieldname]".
      >>
      >If I replace $ar[$i] by $ar everything works fine! Could you pleas
      >help me with that?
      >>
      >
      print $ar[$i]['fieldname'] . "\n";
      or
      print "{$ar[$i]['fieldname']}\n";
      >
      [fieldname] will give you an E_NOTICE if enabled (and it should be on
      your development system).
      >
      A notice is not thrown in this case because the key is already within
      double quotes. The problem is fixed by the necessity to use { }'s when
      using multi-dimensional arrays within double quotes. I normally use this
      method. To get PHP to throw a NOTICE, remove the single quotes from your
      second example.

      --
      Norman
      Registered Linux user #461062

      Comment

      • Peter Pei

        #4
        Re: Multidimensiona l associated array

        print_r will show you the internals

        Comment

        • Peter Pei

          #5
          Re: Multidimensiona l associated array

          no point for you to control the index yourself
          <?php
          $a[] = 2;
          $a[] = 3;
          $a[] = 5;
          print $a;
          print_r($a);
          ?>

          Comment

          • Peter Pei

            #6
            Re: Multidimensiona l associated array

            <?php
            $a = array(array("a" =>1, "b"=>2));
            print "{$a[0]['b']}";
            ?>

            Comment

            • Peter Pei

              #7
              Re: Multidimensiona l associated array

              you obviously forgot to close your }

              Comment

              • Peter Pei

                #8
                Re: Multidimensiona l associated array

                <?php
                $a = array(array("a" =>1, "b"=>2));
                print "{$a[0][b]}";
                ?>
                this gives you notice,

                Comment

                • Peter Pei

                  #9
                  Re: Multidimensiona l associated array

                  php ends the variable name at the most convenience spot

                  Comment

                  • Norman Peelman

                    #10
                    Re: Multidimensiona l associated array

                    Peter Pei wrote:
                    <?php
                    $a = array(array("a" =>1, "b"=>2));
                    print "{$a[0][b]}";
                    ?>
                    this gives you notice,
                    Yes, that's what I said. When using multi-dimensional arrays within
                    double-quoted strings you must use the { }'s... and when you use the {
                    }'s you must use single quotes around the key names. PHP does not search
                    for CONSTANTS within double-quoted strings, there is a very specific
                    reason for it.


                    --
                    Norman
                    Registered Linux user #461062

                    Comment

                    Working...